1
+ import {
2
+ DeepClient ,
3
+ SerialOperation ,
4
+ } from '@deep-foundation/deeplinks/imports/client.js' ;
5
+ import { createSerialOperation } from '@deep-foundation/deeplinks/imports/gql/index.js' ;
6
+ import { Package } from './package.js' ;
7
+
8
+ /**
9
+ * Gets serial operations to insert {@link Package.Notify}
10
+ *
11
+ * @example
12
+ * #### Insert {@link Package.Notify}
13
+ ```ts
14
+ const {serialOperations, linkIds} = await getNotifyInsertSerialOperations({
15
+ deep
16
+ });
17
+ await deep.serial({
18
+ operations: serialOperations
19
+ })
20
+ ```
21
+ * #### Insert {@link Package.Notify} with reserved link id
22
+ ```ts
23
+ const reservedLinkIds = await deep.reserve(2);
24
+ const notifyLinkId = reservedLinkIds.pop();
25
+ const containLinkId = reservedLinkIds.pop();
26
+
27
+ const {serialOperations, linkIds} = await getNotifyInsertSerialOperations({
28
+ deep,
29
+ notify: {
30
+ title,
31
+ body,
32
+ },
33
+ reservedLinkIds: {
34
+ notifyLinkId,
35
+ containLinkId,
36
+ }
37
+ });
38
+ await deep.serial({
39
+ operations: serialOperations
40
+ })
41
+ ```
42
+ */
43
+ export async function getNotifyInsertSerialOperations (
44
+ param : GetNotifyInsertSerialOperationsParam
45
+ ) : Promise < GetNotifyInsertSerialOperationsResult > {
46
+ const {
47
+ deep,
48
+ containValue,
49
+ containerLinkId,
50
+ pushNotificationLinkId,
51
+ deviceLinkId
52
+ } = param ;
53
+ const $package = new Package ( { deep} ) ;
54
+ const reservedLinkIds = await getReservedLinkIds ( ) ;
55
+ const { containLinkId, notifyLinkId } = reservedLinkIds ;
56
+ const typeLinkIds = await getTypeLinkIds ( ) ;
57
+ const { containTypeLinkId, notifyTypeLinkId } = typeLinkIds ;
58
+ const serialOperations = [ ] ;
59
+ const notifyInsertSerialOperation = createSerialOperation ( {
60
+ type : 'insert' ,
61
+ table : 'links' ,
62
+ objects : {
63
+ id : notifyLinkId ,
64
+ type_id : notifyTypeLinkId ,
65
+ from_id : pushNotificationLinkId ,
66
+ to_id : deviceLinkId ,
67
+ } ,
68
+ } ) ;
69
+ serialOperations . push ( notifyInsertSerialOperation ) ;
70
+ if ( containerLinkId !== null ) {
71
+ const containInsertSerialOperation = createSerialOperation ( {
72
+ type : 'insert' ,
73
+ table : 'links' ,
74
+ objects : {
75
+ type_id : containTypeLinkId ,
76
+ from_id : containerLinkId || deep . linkId ,
77
+ to_id : notifyLinkId ,
78
+ } ,
79
+ } ) ;
80
+ serialOperations . push ( containInsertSerialOperation ) ;
81
+ const valueOfContainInsertSerialOperation = createSerialOperation ( {
82
+ type : 'insert' ,
83
+ table : 'strings' ,
84
+ objects : {
85
+ link_id : containLinkId ,
86
+ value : containValue ,
87
+ } ,
88
+ } ) ;
89
+ serialOperations . push ( valueOfContainInsertSerialOperation ) ;
90
+ }
91
+
92
+ return {
93
+ serialOperations,
94
+ linkIds : reservedLinkIds
95
+ } ;
96
+
97
+ type GetReservedLinkIdsResult = Required <
98
+ Exclude <
99
+ GetNotifyInsertSerialOperationsParam [ 'reservedLinkIds' ] ,
100
+ undefined
101
+ >
102
+ > ;
103
+
104
+ async function getReservedLinkIds ( ) : Promise < GetReservedLinkIdsResult > {
105
+ let result : GetReservedLinkIdsResult = {
106
+ containLinkId : 0 ,
107
+ notifyLinkId : 0 ,
108
+ } ;
109
+ const linksToReserveCount =
110
+ Object . keys ( result ) . length -
111
+ Object . keys ( param . reservedLinkIds || { } ) . length ;
112
+ const reservedLinkIds : number [ ] =
113
+ linksToReserveCount > 0 ? await deep . reserve ( linksToReserveCount ) : [ ] ;
114
+ result = {
115
+ containLinkId :
116
+ param . reservedLinkIds ?. containLinkId ?? reservedLinkIds . pop ( ) ! ,
117
+ notifyLinkId :
118
+ param . reservedLinkIds ?. notifyLinkId ?? reservedLinkIds . pop ( ) ! ,
119
+ } ;
120
+ return result ;
121
+ }
122
+
123
+ type GetTypeLinkIdsResult = Required <
124
+ Exclude < GetNotifyInsertSerialOperationsParam [ 'typeLinkIds' ] , undefined >
125
+ > ;
126
+
127
+ async function getTypeLinkIds ( ) : Promise < GetTypeLinkIdsResult > {
128
+ const result : GetTypeLinkIdsResult = {
129
+ containTypeLinkId :
130
+ param . typeLinkIds ?. containTypeLinkId ||
131
+ ( await deep . id ( '@deep-foundation/core' , 'Contain' ) ) ,
132
+ notifyTypeLinkId :
133
+ param . typeLinkIds ?. notifyTypeLinkId ||
134
+ await $package . Notify . id ( ) ,
135
+ } ;
136
+ return result ;
137
+ }
138
+ }
139
+
140
+ export interface GetNotifyInsertSerialOperationsParam {
141
+ /**
142
+ * Reserved link ids that will be used in the serial operations
143
+ */
144
+ reservedLinkIds ?: {
145
+ /**
146
+ * Reserved link id for the notify
147
+ */
148
+ notifyLinkId ?: number ;
149
+ /**
150
+ * Reserved link id for the contain
151
+ */
152
+ containLinkId ?: number ;
153
+ } ;
154
+ /**
155
+ * Link ids of types that will be used in the serial operations
156
+ */
157
+ typeLinkIds ?: {
158
+ /**
159
+ * Link id of the contain type
160
+ */
161
+ containTypeLinkId ?: number ;
162
+ /**
163
+ * Link id of the notify type
164
+ */
165
+ notifyTypeLinkId ?: number ;
166
+ } ;
167
+ /**
168
+ * Deep Client
169
+ */
170
+ deep : DeepClient ;
171
+ /**
172
+ * Link id of the container
173
+ *
174
+ * @remarks
175
+ * If it is null, contain link will not be created
176
+ * @defaultValue {@link GetNotifyInsertSerialOperationsParam.deep.linkId } if not provided or undefined
177
+ */
178
+ containerLinkId ?: number | undefined | null ;
179
+ /**
180
+ * Value of the contain link
181
+ *
182
+ * @remarks
183
+ * If {@link GetNotifyInsertSerialOperationsParam.containerLinkId} is null, this will be ignored
184
+ */
185
+ containValue ?: string | undefined ;
186
+ /**
187
+ * Link id of the push notification
188
+ */
189
+ pushNotificationLinkId : number ;
190
+ /**
191
+ * Link id of the device
192
+ */
193
+ deviceLinkId : number ;
194
+ }
195
+
196
+ export interface GetNotifyInsertSerialOperationsResult {
197
+ serialOperations : Array < SerialOperation > ,
198
+ linkIds : Required < Exclude < GetNotifyInsertSerialOperationsParam [ 'reservedLinkIds' ] , undefined > > ,
199
+ }
0 commit comments