-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathapi.ts
More file actions
60 lines (52 loc) · 2.32 KB
/
Copy pathapi.ts
File metadata and controls
60 lines (52 loc) · 2.32 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
'use server';
import { ServiceError } from "@/lib/serviceError";
import { withAuthV2 } from "@/withAuthV2";
import { env, getEntitlements } from "@sourcebot/shared";
import { AccountPermissionSyncJobStatus } from "@sourcebot/db";
import { StatusCodes } from "http-status-codes";
import { ErrorCode } from "@/lib/errorCodes";
import { sew } from "@/actions";
export interface PermissionSyncStatusResponse {
hasPendingFirstSync: boolean;
}
/**
* Returns whether a user has a account that has it's permissions
* synced for the first time.
*/
export const getPermissionSyncStatus = async (): Promise<PermissionSyncStatusResponse | ServiceError> => sew(async () =>
withAuthV2(async ({ prisma, user }) => {
const entitlements = getEntitlements();
if (!entitlements.includes('permission-syncing')) {
return {
statusCode: StatusCodes.FORBIDDEN,
errorCode: ErrorCode.INSUFFICIENT_PERMISSIONS,
message: "Permission syncing is not enabled for your license",
} satisfies ServiceError;
}
const accounts = await prisma.account.findMany({
where: {
userId: user.id,
provider: { in: ['github', 'gitlab', 'bitbucket-cloud', 'bitbucket-server'] }
},
include: {
permissionSyncJobs: {
orderBy: { createdAt: 'desc' },
take: 1,
}
}
});
const activeStatuses: AccountPermissionSyncJobStatus[] = [
AccountPermissionSyncJobStatus.PENDING,
AccountPermissionSyncJobStatus.IN_PROGRESS
];
const hasPendingFirstSync = env.EXPERIMENT_EE_PERMISSION_SYNC_ENABLED === 'true' &&
accounts.some(account =>
account.permissionSyncedAt === null &&
// @note: to handle the case where the permission sync job
// has not yet been scheduled for a new account, we consider
// accounts with no permission sync jobs as having a pending first sync.
(account.permissionSyncJobs.length === 0 || (account.permissionSyncJobs.length > 0 && activeStatuses.includes(account.permissionSyncJobs[0].status)))
)
return { hasPendingFirstSync } satisfies PermissionSyncStatusResponse;
})
)