-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathroute.ts
More file actions
63 lines (54 loc) · 2.07 KB
/
route.ts
File metadata and controls
63 lines (54 loc) · 2.07 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
'use server';
import { apiHandler } from "@/lib/apiHandler";
import { serviceErrorResponse } from "@/lib/serviceError";
import { isServiceError } from "@/lib/utils";
import { withAuthV2 } from "@/withAuthV2";
import { getEntitlements } from "@sourcebot/shared";
import { AccountPermissionSyncJobStatus } from "@sourcebot/db";
import { StatusCodes } from "http-status-codes";
import { ErrorCode } from "@/lib/errorCodes";
export interface PermissionSyncStatusResponse {
hasPendingFirstSync: boolean;
}
/**
* Returns whether a user has a account that has it's permissions
* synced for the first time.
*/
export const GET = apiHandler(async () => {
const entitlements = getEntitlements();
if (!entitlements.includes('permission-syncing')) {
return serviceErrorResponse({
statusCode: StatusCodes.FORBIDDEN,
errorCode: ErrorCode.INSUFFICIENT_PERMISSIONS,
message: "Permission syncing is not enabled for your license",
});
}
const result = await withAuthV2(async ({ prisma, user }) => {
const accounts = await prisma.account.findMany({
where: {
userId: user.id,
provider: { in: ['github', 'gitlab', 'bitbucket-cloud'] }
},
include: {
permissionSyncJobs: {
orderBy: { createdAt: 'desc' },
take: 1,
}
}
});
const activeStatuses: AccountPermissionSyncJobStatus[] = [
AccountPermissionSyncJobStatus.PENDING,
AccountPermissionSyncJobStatus.IN_PROGRESS
];
const hasPendingFirstSync = accounts.some(account =>
account.permissionSyncedAt === null &&
account.permissionSyncJobs.length > 0 &&
activeStatuses.includes(account.permissionSyncJobs[0].status)
);
return { hasPendingFirstSync } satisfies PermissionSyncStatusResponse;
});
if (isServiceError(result)) {
return serviceErrorResponse(result);
}
return Response.json(result, { status: StatusCodes.OK });
});