feat: attributes filter / refactor of user data table (#17014)

* WIP restored from .git cache

* fix exports

* sortable row model

* feat column visibility component

* wip filters with nuqs

* pull in unique values from table into filters

* correctly assign filters via v/f

* inital selection bar refactor

* data-table selection bar + optmistic update of delete

* dynamic link

* migrate member list table to new data-table

* total list shows filtered value > db valuie

* add filters for attributes

* type errors

* make content bigger on lg

* add mb-6 to teams user datatable to match spacing spec

* correctly render multi-badge

* fix: masss asignment optimistic UI

* fix type errors

* remove log

* fix toolbar type error

* chore: Remove debug artifact

* type errors

* Update apps/web/public/static/locales/en/common.json

* use max-w-fit

* chore: Remove unused translation now we don't specify 'mass' in assign

* perf: fix: use the onBlur event to prevent focus loss whilst the list is rerendering

* Move the data-table exports together in the main barrel, then import

* fix exports that were lost in a merge

* fix exports that were lost in a merge

* fix groupteammapping/availbilityslider

* fix overflow problems

* add scrollbar-thin class

* fix type error

* user serverside values for faceted filters

* pass filters to serverside

* filter serverside

* fix team server side filter

* add loaded x of y

* attributes icon change

* correct implementation for text/input attr optimistic

* type check fixes

* fix platform checks

* fix types again

* fix types again

* fix types again

* add use client

* add use client

* fix-types

* fix: Add missing translation in EN

* fix e2e tests via testid

* fix e2e tests via testid

* fix: Member invite popup not popping up

* Update copyInviteLink to new-member-button testid

* Hopefully fix test ids this time

* fix: Use the right buttons on the right pages

---------

Co-authored-by: Alex van Andel <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-10-15 17:45:11 +00:00
1 parent 0b914bef79
commit 7e44e686e8
43 files changed
+1462 -1049

No files matched your search

@@ -169,4 +169,11 @@ export const viewerOrganizationsRouter = router({
);
return handler(opts);
}),
getFacetedValues: authedProcedure.query(async (opts) => {
const handler = await importHandler(
namespaced("getFacetedValues"),
() => import("./getFacetedValues.handler")
);
return handler(opts);
}),
});
@@ -0,0 +1,54 @@
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../trpc";
type DeleteOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
};
export const getFacetedValuesHandler = async ({ ctx }: DeleteOptions) => {
const { user } = ctx;
const organizationId = user.organization?.id;
if (!organizationId) {
throw new TRPCError({ code: "NOT_FOUND", message: "Organization not found" });
}
const [teams, attributes] = await Promise.all([
prisma.team.findMany({
where: {
parentId: organizationId,
},
select: {
id: true,
name: true,
},
}),
prisma.attribute.findMany({
where: { teamId: organizationId },
select: {
id: true,
name: true,
options: {
select: {
value: true,
},
distinct: "value",
},
},
}),
]);
return {
teams: teams,
attributes: attributes,
roles: [MembershipRole.OWNER, MembershipRole.ADMIN, MembershipRole.MEMBER],
};
};
export default getFacetedValuesHandler;
@@ -0,0 +1,7 @@
import { z } from "zod";
export const ZGetFacetedValuesInput = z.object({
include: z.enum(["users", "roles", "teams", "attributes"]).array().optional(),
});
export type TGetFacetedValuesInputSchema = z.infer<typeof ZGetFacetedValuesInput>;
@@ -1,5 +1,7 @@
import { UserRepository } from "@calcom/lib/server/repository/user";
import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
import type { MembershipRole } from "@calcom/prisma/enums";
import { TRPCError } from "@trpc/server";
@@ -16,6 +18,8 @@ type GetOptions = {
export const listMembersHandler = async ({ ctx, input }: GetOptions) => {
const organizationId = ctx.user.organizationId ?? ctx.user.profiles[0].organizationId;
const searchTerm = input.searchTerm;
const expand = input.expand;
const filters = input.filters || [];
if (!organizationId) {
throw new TRPCError({ code: "NOT_FOUND", message: "User is not part of any organization." });
@@ -39,30 +43,56 @@ export const listMembersHandler = async ({ ctx, input }: GetOptions) => {
},
});
// I couldnt get this query to work direct on membership table
const teamMembers = await prisma.membership.findMany({
where: {
teamId: organizationId,
user: {
isPlatformManaged: false,
},
...(searchTerm && {
user: {
OR: [
{
email: {
contains: searchTerm,
},
},
{
username: {
contains: searchTerm,
},
},
],
},
}),
const whereClause = {
user: {
isPlatformManaged: false,
},
teamId: organizationId,
...(searchTerm && {
user: {
OR: [{ email: { contains: searchTerm } }, { username: { contains: searchTerm } }],
},
}),
} as Prisma.MembershipWhereInput;
filters.forEach((filter) => {
switch (filter.id) {
case "role":
whereClause.role = { in: filter.value as MembershipRole[] };
break;
case "teams":
whereClause.user = {
teams: {
some: {
team: {
name: {
in: filter.value,
},
},
},
},
};
break;
// We assume that if the filter is not one of the above, it must be an attribute filter
default:
whereClause.AttributeToUser = {
some: {
attributeOption: {
attribute: {
id: filter.id,
},
value: {
in: filter.value,
},
},
},
};
break;
}
});
const teamMembers = await prisma.membership.findMany({
where: whereClause,
select: {
id: true,
role: true,
@@ -106,6 +136,25 @@ export const listMembersHandler = async ({ ctx, input }: GetOptions) => {
const members = await Promise.all(
teamMembers?.map(async (membership) => {
const user = await UserRepository.enrichUserWithItsProfile({ user: membership.user });
let attributes;
if (expand?.includes("attributes")) {
attributes = await prisma.attributeOption.findMany({
where: {
assignedUsers: {
some: {
memberId: membership.id,
},
},
},
orderBy: {
attribute: {
name: "asc",
},
},
});
}
return {
id: user.id,
username: user.username,
@@ -126,6 +175,7 @@ export const listMembersHandler = async ({ ctx, input }: GetOptions) => {
slug: team.team.slug,
};
}),
attributes,
};
}) || []
);
@@ -1,9 +1,20 @@
import { z } from "zod";
const expandableColumns = z.enum(["attributes"]);
export const ZListMembersSchema = z.object({
limit: z.number().min(1).max(100),
cursor: z.number().nullish(),
searchTerm: z.string().optional(),
expand: z.array(expandableColumns).optional(),
filters: z
.array(
z.object({
id: z.string(),
value: z.array(z.string()),
})
)
.optional(),
});
export type TListMembersSchema = z.infer<typeof ZListMembersSchema>;