Skip to content

Scheduling infrastructure for absolutely everyone.

Script to reassign attributes (#24496)

Joe Au-Yeung78c2a693896f
diff --git a/scripts/reassign-attributes.ts b/scripts/reassign-attributes.ts
new file mode 100644
index 00000000000..f9cb0629078
--- /dev/null
+++ b/scripts/reassign-attributes.ts
@@ -0,0 +1,41 @@
+import { prisma } from "@calcom/prisma";
+
+const reassignAttributes = async () => {
+ const orgId = 0;
+
+ // Add attribute option id that we want to search against
+ const lookupAttributeId = "";
+
+ // Add attribute option ids that we want to assign to users
+ const attributeIdsToAssign = [];
+
+ // Find all users who have the lookupAttributeId
+ const usersWithAttribute = await prisma.attributeToUser.findMany({
+ where: {
+ attributeOptionId: lookupAttributeId,
+ member: {
+ teamId: orgId,
+ },
+ },
+ select: {
+ memberId: true,
+ },
+ });
+
+ console.log(`Found ${usersWithAttribute.length} users with attribute ${lookupAttributeId}`);
+
+ // Assign the new attributes to these users
+ for (const attributeId of attributeIdsToAssign) {
+ const results = await prisma.attributeToUser.createMany({
+ data: usersWithAttribute.map((user) => ({
+ memberId: user.memberId,
+ attributeOptionId: attributeId,
+ })),
+ skipDuplicates: true, // Skip if user already has this attribute
+ });
+
+ console.log(`Assigned attribute ${attributeId} to ${results.count} users`);
+ }
+};
+
+export default reassignAttributes();