diff --git a/apps/web/app/api/v1/client/[environmentId]/people/[userId]/route.ts b/apps/web/app/api/v1/client/[environmentId]/people/[userId]/route.ts index 56cab874fb..3e6a6e7ac7 100644 --- a/apps/web/app/api/v1/client/[environmentId]/people/[userId]/route.ts +++ b/apps/web/app/api/v1/client/[environmentId]/people/[userId]/route.ts @@ -31,6 +31,9 @@ export async function POST(req: Request, context: Context): Promise { ); } + // remove userId from attributes because it is not allowed to be updated + const { userId: userIdAttr, ...updatedAttributes } = inputValidation.data.attributes; + let person = await getPersonByUserId(environmentId, userId); if (!person) { @@ -40,11 +43,10 @@ export async function POST(req: Request, context: Context): Promise { } // Check if the person is already up to date - const updatedAtttributes = inputValidation.data.attributes; const oldAttributes = person.attributes; let isUpToDate = true; - for (const key in updatedAtttributes) { - if (updatedAtttributes[key] !== oldAttributes[key]) { + for (const key in updatedAttributes) { + if (updatedAttributes[key] !== oldAttributes[key]) { isUpToDate = false; break; } diff --git a/packages/database/data-migrations/20240408123456_userid_migration/data-migration.ts b/packages/database/data-migrations/20240408123456_userid_migration/data-migration.ts index af414ca3cf..e80923cc0b 100644 --- a/packages/database/data-migrations/20240408123456_userid_migration/data-migration.ts +++ b/packages/database/data-migrations/20240408123456_userid_migration/data-migration.ts @@ -6,55 +6,44 @@ async function main() { await prisma.$transaction( async (tx) => { // get all the persons that have an attribute class with the name "userId" - const personsWithUserIdAttribute = await tx.person.findMany({ + const userIdAttributeClasses = await tx.attributeClass.findMany({ where: { - attributes: { - some: { - attributeClass: { - name: "userId", - }, - }, - }, + name: "userId", }, include: { attributes: { - include: { attributeClass: true }, + include: { person: true }, }, }, }); - for (let person of personsWithUserIdAttribute) { - // If the person already has a userId, skip it - if (person.userId) { - continue; - } - - const userIdAttributeValue = person.attributes.find((attribute) => { - if (attribute.attributeClass.name === "userId") { - return attribute; + for (let attributeClass of userIdAttributeClasses) { + for (let attribute of attributeClass.attributes) { + if (attribute.person.userId) { + continue; } - }); - if (!userIdAttributeValue) { - continue; + await tx.person.update({ + where: { + id: attribute.personId, + }, + data: { + userId: attribute.value, + }, + }); } - - await tx.person.update({ - where: { - id: person.id, - }, - data: { - userId: userIdAttributeValue.value, - }, - }); } + console.log("Migrated userIds to the person table."); + // Delete all attributeClasses with the name "userId" await tx.attributeClass.deleteMany({ where: { name: "userId", }, }); + + console.log("Deleted attributeClasses with the name 'userId'."); }, { timeout: 60000 * 3, // 3 minutes diff --git a/packages/js-core/src/lib/person.ts b/packages/js-core/src/lib/person.ts index 391a39c23c..b5a8bf6107 100644 --- a/packages/js-core/src/lib/person.ts +++ b/packages/js-core/src/lib/person.ts @@ -147,6 +147,11 @@ export const setPersonAttribute = async ( key: string, value: any ): Promise> => { + if (key === "userId") { + logger.error("Setting userId is no longer supported. Please set the userId in the init call instead."); + return okVoid(); + } + logger.debug("Setting attribute: " + key + " to value: " + value); // check if attribute already exists with this value if (isExistingAttribute(key, value.toString())) {