Skip to content

Commit 78c2a69

Browse files
authored
Script to reassign attributes (calcom#24496)
1 parent 5a483e7 commit 78c2a69

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

scripts/reassign-attributes.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { prisma } from "@calcom/prisma";
2+
3+
const reassignAttributes = async () => {
4+
const orgId = 0;
5+
6+
// Add attribute option id that we want to search against
7+
const lookupAttributeId = "";
8+
9+
// Add attribute option ids that we want to assign to users
10+
const attributeIdsToAssign = [];
11+
12+
// Find all users who have the lookupAttributeId
13+
const usersWithAttribute = await prisma.attributeToUser.findMany({
14+
where: {
15+
attributeOptionId: lookupAttributeId,
16+
member: {
17+
teamId: orgId,
18+
},
19+
},
20+
select: {
21+
memberId: true,
22+
},
23+
});
24+
25+
console.log(`Found ${usersWithAttribute.length} users with attribute ${lookupAttributeId}`);
26+
27+
// Assign the new attributes to these users
28+
for (const attributeId of attributeIdsToAssign) {
29+
const results = await prisma.attributeToUser.createMany({
30+
data: usersWithAttribute.map((user) => ({
31+
memberId: user.memberId,
32+
attributeOptionId: attributeId,
33+
})),
34+
skipDuplicates: true, // Skip if user already has this attribute
35+
});
36+
37+
console.log(`Assigned attribute ${attributeId} to ${results.count} users`);
38+
}
39+
};
40+
41+
export default reassignAttributes();

0 commit comments

Comments
 (0)