From d84f08c998623f0fbb5975a684a8830327294260 Mon Sep 17 00:00:00 2001 From: sean-brydon <55134778+sean-brydon@users.noreply.github.com> Date: Thu, 22 Aug 2024 15:28:49 +0100 Subject: [PATCH] actually delete the item if no options or input is found (#16298) Co-authored-by: Keith Williams --- .../assignUserToAttribute.handler.ts | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/trpc/server/routers/viewer/attributes/assignUserToAttribute.handler.ts b/packages/trpc/server/routers/viewer/attributes/assignUserToAttribute.handler.ts index d00d9f236e..064f86ae62 100644 --- a/packages/trpc/server/routers/viewer/attributes/assignUserToAttribute.handler.ts +++ b/packages/trpc/server/routers/viewer/attributes/assignUserToAttribute.handler.ts @@ -23,15 +23,11 @@ const assignUserToAttributeHandler = async ({ input, ctx }: GetOptions) => { }); } - // TODO: We need to also empty the users assignemnts for IDs that are not in in this filteredAttributes list - // Filter out attributes that don't have a value or options set - const filteredAttributes = input.attributes.filter((attribute) => attribute.value || attribute.options); - // Ensure this organization can access these attributes and attribute options const attributes = await prisma.attribute.findMany({ where: { id: { - in: filteredAttributes.map((attribute) => attribute.id), + in: input.attributes.map((attribute) => attribute.id), }, teamId: org.id, }, @@ -42,7 +38,7 @@ const assignUserToAttributeHandler = async ({ input, ctx }: GetOptions) => { }, }); - if (attributes.length !== filteredAttributes.length) { + if (attributes.length !== input.attributes.length) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You do not have access to these attributes", @@ -94,7 +90,7 @@ const assignUserToAttributeHandler = async ({ input, ctx }: GetOptions) => { // const promises: Promise<{ id: string }>[] = []; - filteredAttributes.map(async (attribute) => { + input.attributes.map(async (attribute) => { // TEXT, NUMBER if (attribute.value && !attribute.options) { const valueAsString = String(attribute.value); @@ -204,6 +200,20 @@ const assignUserToAttributeHandler = async ({ input, ctx }: GetOptions) => { }); }); } + + // Delete the attribute from the user + if (!attribute.value && !attribute.options) { + await prisma.attributeToUser.deleteMany({ + where: { + memberId: membership.id, + attributeOption: { + attribute: { + id: attribute.id, + }, + }, + }, + }); + } }); return {