-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathroute.ts
More file actions
81 lines (71 loc) · 2.8 KB
/
route.ts
File metadata and controls
81 lines (71 loc) · 2.8 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
'use server';
import { withAuthV2, withMinimumOrgRole } from "@/withAuthV2";
import { OrgRole } from "@sourcebot/db";
import { isServiceError } from "@/lib/utils";
import { serviceErrorResponse } from "@/lib/serviceError";
import { createLogger } from "@sourcebot/logger";
import { getAuditService } from "@/ee/features/audit/factory";
const logger = createLogger('ee-users-api');
const auditService = getAuditService();
export const GET = async () => {
const result = await withAuthV2(async ({ prisma, org, role, user }) => {
return withMinimumOrgRole(role, OrgRole.OWNER, async () => {
try {
const memberships = await prisma.userToOrg.findMany({
where: {
orgId: org.id,
},
include: {
user: true,
},
});
const usersWithActivity = await Promise.all(
memberships.map(async (membership) => {
const lastActivity = await prisma.audit.findFirst({
where: {
actorId: membership.user.id,
actorType: 'user',
orgId: org.id,
},
orderBy: {
timestamp: 'desc',
},
select: {
timestamp: true,
},
});
return {
id: membership.user.id,
name: membership.user.name,
email: membership.user.email,
role: membership.role,
createdAt: membership.user.createdAt,
lastActivityAt: lastActivity?.timestamp ?? null,
};
})
);
await auditService.createAudit({
action: "user.list",
actor: {
id: user.id,
type: "user"
},
target: {
id: org.id.toString(),
type: "org"
},
orgId: org.id
});
logger.info('Fetched users list', { count: usersWithActivity.length });
return usersWithActivity;
} catch (error) {
logger.error('Error fetching users', { error });
throw error;
}
});
});
if (isServiceError(result)) {
return serviceErrorResponse(result);
}
return Response.json(result);
};