feat: weights per attribute UI (#18109)

* wip attribut weights

* edit sheet and display in table

* add shrink so badges dont split

* push changes to support weights on csv export

* fix typing issue

* type error

* fall back to 100 weight

---------

Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
sean-brydonandUdit Takkar authored and GitHub committed 2024-12-12 11:54:10 +00:00
1 parent b6d6b7cdb4
commit ee3c08918a
18 files changed
+190 -67

No files matched your search

@@ -191,6 +191,7 @@ const assignUserToAttributeHandler = async ({ input, ctx }: GetOptions) => {
create: {
memberId: membership.id,
attributeOptionId: option.value,
weight: option.weight,
},
update: {}, // No update needed if it already exists
select: {
@@ -2,7 +2,9 @@ import { z } from "zod";
const attributeSchema = z.object({
id: z.string(),
options: z.array(z.object({ label: z.string().optional(), value: z.string() })).optional(),
options: z
.array(z.object({ label: z.string().optional(), value: z.string(), weight: z.number().optional() }))
.optional(),
value: z.string().optional(),
});
@@ -52,6 +52,7 @@ const editAttributesHandler = async ({ input, ctx }: GetOptions) => {
type: input.type,
teamId: org.id,
isLocked: input.isLocked,
isWeightsEnabled: input.isWeightsEnabled,
},
select: {
id: true,
@@ -5,6 +5,7 @@ export const editAttributeSchema = z.object({
name: z.string(),
type: z.enum(["TEXT", "NUMBER", "SINGLE_SELECT", "MULTI_SELECT"]),
isLocked: z.boolean().optional(),
isWeightsEnabled: z.boolean().optional(),
options: z.array(
z.object({
value: z.string(),
@@ -34,6 +34,7 @@ const getAttributeHandler = async ({ input, ctx }: GetOptions) => {
name: true,
type: true,
isLocked: true,
isWeightsEnabled: true,
options: {
select: {
id: true,
@@ -65,6 +66,7 @@ const getAttributeHandler = async ({ input, ctx }: GetOptions) => {
id: z.string(),
name: z.string(),
isLocked: z.boolean().optional(),
isWeightsEnabled: z.boolean().optional(),
type: z.enum(["TEXT", "NUMBER", "SINGLE_SELECT", "MULTI_SELECT"]),
options: z.array(
z.object({
@@ -21,6 +21,7 @@ type GroupedAttribute = {
id: string;
slug: string;
value: string;
weight: number | null;
createdByDSyncId: string | null;
}[];
};
@@ -77,13 +78,14 @@ const getByUserIdHandler = async ({ input, ctx }: GetOptions) => {
},
},
createdByDSyncId: true,
weight: true,
},
});
const groupedAttributes = userAttributes.reduce<GroupedAttribute[]>((acc, assignment) => {
const { attributeOption, createdByDSyncId } = assignment;
const { attributeOption, createdByDSyncId, weight } = assignment;
const { attribute: attrInfo, ...optionInfo } = attributeOption;
const optionInfoWithCreatedByDSyncId = { ...optionInfo, createdByDSyncId };
const optionInfoWithCreatedByDSyncId = { ...optionInfo, createdByDSyncId, weight };
const existingGroup = acc.find((group) => group.id === attrInfo.id);
if (existingGroup) {
@@ -177,20 +177,29 @@ export const listMembersHandler = async ({ ctx, input }: GetOptions) => {
let attributes;
if (expand?.includes("attributes")) {
attributes = await prisma.attributeOption.findMany({
where: {
assignedUsers: {
some: {
memberId: membership.id,
attributes = await prisma.attributeToUser
.findMany({
where: {
memberId: membership.id,
},
select: {
attributeOption: true,
weight: true,
},
orderBy: {
attributeOption: {
attribute: {
name: "asc",
},
},
},
},
orderBy: {
attribute: {
name: "asc",
},
},
});
})
.then((assignedUsers) =>
assignedUsers.map((au) => ({
...au.attributeOption,
weight: au.weight ?? 100,
}))
);
}
return {