/ src / lib / utils / wave / notifications.ts
notifications.ts
 1  import z from 'zod';
 2  import { authenticatedCall } from './call';
 3  import parseRes from './utils/parse-res';
 4  
 5  export enum WORKFLOW_ID {
 6    WELCOME = 'welcome',
 7    REPO_APPLIED_TO_WAVE = 'repo-applied-to-wave-program',
 8    REPO_APPLICATION_APPROVED = 'repo-approved-for-wave-program',
 9    REPO_APPLICATION_REJECTED = 'repo-declined-for-wave-program',
10    ISSUE_APPLICATION_RECEIVED = 'issue-application-received',
11    ISSUE_APPLICATION_APPROVED = 'issue-application-approved',
12    COMPLIMENT_RECEIVED = 'compliment-received',
13    CONTRIBUTOR_UNASSIGNED = 'contributor-unassigned',
14    CONTRIBUTOR_WITHDREW = 'contributor-withdrew',
15    ORG_ISSUE_APPLICATION_RECEIVED = 'org-issue-application-received',
16    ISSUE_POINTS_RECEIVED = 'issue-points-received',
17    OPEN_ISSUE_REMINDER_CONTRIBUTORS = 'open-issue-reminder-contributors',
18    OPEN_ISSUE_REMINDER_MAINTAINERS = 'open-issue-reminder-maintainers',
19  }
20  
21  export async function getNotificationPreferences(f = fetch) {
22    return parseRes(
23      z.object({
24        preferences: z.array(
25          z.object({
26            workflowId: z.enum(Object.values(WORKFLOW_ID)).or(z.string()),
27            channels: z.object({
28              email: z.boolean(),
29              inApp: z.boolean(),
30            }),
31          }),
32        ),
33      }),
34      await authenticatedCall(f, `/api/user/preferences`),
35    );
36  }
37  
38  export async function patchNotificationPreference(
39    f = fetch,
40    workflowId: WORKFLOW_ID,
41    channels: {
42      email?: boolean;
43      inApp?: boolean;
44    },
45  ) {
46    await authenticatedCall(f, `/api/user/preferences/${workflowId}`, {
47      method: 'PUT',
48      body: JSON.stringify({ channels }),
49    });
50  }