-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepository.ts
More file actions
192 lines (176 loc) · 4.92 KB
/
Copy pathrepository.ts
File metadata and controls
192 lines (176 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { and, eq, inArray, isNull, type SQL, sql } from "drizzle-orm";
import { db, schema } from "@/db/index.js";
import { dbAction } from "@/lib/helpers.js";
export const getOrganizationMembers = dbAction(
async (
managedEntityId: number,
filters: {
userId?: number;
},
) => {
const userFilterClauses: SQL[] = [];
if (filters.userId != null) {
// User specific search
userFilterClauses.push(eq(schema.user.id, filters.userId));
} else {
// Global search
userFilterClauses.push(
inArray(
schema.user.id,
db
.select({ id: schema.userRole.userId })
.from(schema.userRole)
.where(
and(
eq(schema.userRole.managedEntityId, managedEntityId),
isNull(schema.userRole.deletedAt),
),
),
),
);
}
return await db.query.user.findMany({
where: and(isNull(schema.user.deletedAt), ...userFilterClauses),
columns: {
id: true,
fullName: true,
email: true,
},
with: {
roles: {
where: and(
eq(schema.userRole.managedEntityId, managedEntityId),
isNull(schema.userRole.deletedAt),
),
columns: {
id: true,
roleId: true,
isActive: true,
},
},
},
});
},
);
export const assignOrganizationMemberRoles = dbAction(
async (data: { managedEntityId: number; userId: number; roleIds: number[] }) => {
return await db.transaction(async (tx) => {
const userRoles = await tx
.select({
id: schema.userRole.id,
roleId: schema.userRole.roleId,
})
.from(schema.userRole)
.where(
and(
eq(schema.userRole.managedEntityId, data.managedEntityId),
eq(schema.userRole.userId, data.userId),
isNull(schema.userRole.deletedAt),
),
);
const newRoleIds = new Set(data.roleIds);
const roleIdToId = new Map<number, number>();
const toBeDeletedPks: number[] = [];
for (const userRole of userRoles) {
if (newRoleIds.has(userRole.roleId)) {
roleIdToId.set(userRole.roleId, userRole.id);
} else {
toBeDeletedPks.push(userRole.id);
}
}
const deletedUserRoles = userRoles.filter((ur) => toBeDeletedPks.includes(ur.id));
const existingRoleIds = new Set(userRoles.map((ur) => ur.roleId));
const toBeAdded = data.roleIds.filter((roleId) => !existingRoleIds.has(roleId));
if (toBeDeletedPks.length > 0) {
await tx
.update(schema.userRole)
.set({ deletedAt: sql`now()` })
.where(inArray(schema.userRole.id, toBeDeletedPks));
}
if (toBeAdded.length > 0) {
const inserted = await tx
.insert(schema.userRole)
.values(
toBeAdded.map((roleId) => ({
managedEntityId: data.managedEntityId,
userId: data.userId,
roleId: roleId,
})),
)
.returning({ id: schema.userRole.id, roleId: schema.userRole.roleId });
for (const row of inserted) {
roleIdToId.set(row.roleId, row.id);
}
}
for (const oldUr of deletedUserRoles) {
const newUrId = roleIdToId.get(oldUr.roleId);
if (newUrId != null) {
await tx
.update(schema.workflowInstanceStepAssignment)
.set({ userRoleId: newUrId })
.where(
and(
eq(schema.workflowInstanceStepAssignment.userRoleId, oldUr.id),
eq(schema.workflowInstanceStepAssignment.status, "pending"),
isNull(schema.workflowInstanceStepAssignment.deletedAt),
),
);
}
}
return data.roleIds
.map((roleId) => ({ id: roleIdToId.get(roleId), roleId }))
.filter((entry): entry is { id: number; roleId: number } => entry.id != null);
});
},
);
export const deleteOrganizationMember = dbAction(
async (data: { managedEntityId: number; userId: number }) => {
return await db
.update(schema.userRole)
.set({ deletedAt: sql`now()` })
.where(
and(
eq(schema.userRole.managedEntityId, data.managedEntityId),
eq(schema.userRole.userId, data.userId),
isNull(schema.userRole.deletedAt),
),
)
.returning({ id: schema.userRole.id });
},
);
export const findOrganizerMemberWithRole = dbAction(
async (data: { organizationId: number; userId: number; roleId: number }) => {
const [userRole] = await db
.select({ id: schema.userRole.id })
.from(schema.userRole)
.innerJoin(
schema.managedEntity,
and(
eq(schema.managedEntity.id, schema.userRole.managedEntityId),
eq(schema.managedEntity.managedEntityType, "organization"),
isNull(schema.managedEntity.deletedAt),
),
)
.innerJoin(
schema.organization,
and(
eq(schema.organization.id, schema.managedEntity.refId),
eq(schema.organization.id, data.organizationId),
isNull(schema.organization.deletedAt),
),
)
.innerJoin(
schema.role,
and(eq(schema.role.id, schema.userRole.roleId), isNull(schema.role.deletedAt)),
)
.where(
and(
eq(schema.userRole.userId, data.userId),
eq(schema.userRole.roleId, data.roleId),
isNull(schema.userRole.deletedAt),
),
)
.limit(1);
return userRole;
},
);