/
Invitations.ts
99 lines (93 loc) · 3.61 KB
/
Invitations.ts
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
import { params, types } from 'typed-graphqlify'
import { RoomKeys, RoomOption, RoomResult, buildRoomQuery } from './Rooms'
import { OrganizationKeys, OrganizationType, buildOrganizationQuery } from './Organizations'
import { pick, preprocessArgs } from '../Utils'
import { ContactType, InvitationStatus, MutationSendInvitationArgs } from '../generated/graphql'
import { RoomMemberKeys } from './RoomMembers'
import { UserKeys } from './Users'
import { UserProfileKeys } from './UserProfiles'
import { EmailKeys } from './Emails'
export const Invitation = {
id: types.string,
contactType: types.oneOf(ContactType),
contactInfo: types.string,
contactFullName: types.string,
status: types.oneOf(InvitationStatus)
}
export type InvitationType = typeof Invitation
export type InvitationKeys = keyof InvitationType
export type InvitationResult<
I extends InvitationKeys | null,
R extends RoomKeys | null,
R_O extends OrganizationKeys | null,
R_RM extends RoomMemberKeys | null,
R_RM_U extends UserKeys | null,
R_RM_U_UP extends UserProfileKeys | null,
R_RM_U_UP_E extends EmailKeys | null,
O extends OrganizationKeys | null,
> =
([I] extends [InvitationKeys] ? Pick<InvitationType, I> : {}) &
([R] extends [RoomKeys] ? { room: RoomResult<R, R_O, R_RM, R_RM_U, R_RM_U_UP, R_RM_U_UP_E> } : {}) &
([O] extends [OrganizationKeys] ? { profile: Pick<OrganizationType, O> } : {})
export type InvitationOption<R, R_O, RM, RM_U, RM_U_UP, R_RM_U_UP_E, O> = {
room?: { fields: R[], with: RoomOption<R_O, RM, RM_U, RM_U_UP, R_RM_U_UP_E> },
organization?: { fields: O[] }
}
function resolveOption<
R extends RoomKeys | null,
R_O extends OrganizationKeys | null,
R_RM extends RoomMemberKeys | null,
R_RM_U extends UserKeys | null,
R_RM_U_UP extends UserProfileKeys | null,
R_RM_U_UP_E extends EmailKeys | null,
O extends OrganizationKeys | null
>(option: InvitationOption<R, R_O, R_RM, R_RM_U, R_RM_U_UP, R_RM_U_UP_E, O>) {
const org = option.organization ?
{ organization: buildOrganizationQuery(option.organization.fields as any) } : {}
const room = option.room ?
{ room: buildRoomQuery(option.room.fields as any, option.room.with) } : {}
return { ...org, ...room }
}
/**
* @ignore
*/
export function buildInvitationQuery<
I extends InvitationKeys,
R extends RoomKeys | null,
R_O extends OrganizationKeys | null,
R_RM extends RoomMemberKeys | null,
R_RM_U extends UserKeys | null,
R_RM_U_UP extends UserProfileKeys | null,
R_RM_U_UP_E extends EmailKeys | null,
O extends OrganizationKeys | null
>(
fields: I[],
option?: InvitationOption<R, R_O, R_RM, R_RM_U, R_RM_U_UP, R_RM_U_UP_E, O>
): InvitationResult<I, R, R_O, R_RM, R_RM_U, R_RM_U_UP, R_RM_U_UP_E, O> {
const pickedField: any = pick(Invitation, fields)
const resolvedOption = option ? resolveOption(option) : {}
return { ...pickedField, ...resolvedOption }
}
/**
* @ignore
*/
export function buildSendInvitationMutation<
I extends InvitationKeys,
R extends RoomKeys | null,
R_O extends OrganizationKeys | null,
R_RM extends RoomMemberKeys | null,
R_RM_U extends UserKeys | null,
R_RM_U_UP extends UserProfileKeys | null,
R_RM_U_UP_E extends EmailKeys | null,
O extends OrganizationKeys | null
>(
fields: I[],
args: MutationSendInvitationArgs,
option?: InvitationOption<R, R_O, R_RM, R_RM_U, R_RM_U_UP, R_RM_U_UP_E, O>
): { sendInvitation: InvitationResult<I, R, R_O, R_RM, R_RM_U, R_RM_U_UP, R_RM_U_UP_E, O> } {
const pickedFields: any = pick(Invitation, fields)
const resolvedOption = option ? resolveOption(option) : {}
return {
sendInvitation: params(preprocessArgs(args), { ...pickedFields, ...resolvedOption })
}
}