@voyantjs/booking-requirements 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,374 @@
1
+ import { and, asc, desc, eq, sql } from "drizzle-orm";
2
+ import { bookingAnswers, bookingQuestionExtraTriggers, bookingQuestionOptions, bookingQuestionOptionTriggers, bookingQuestionUnitTriggers, optionBookingQuestions, productBookingQuestions, productContactRequirements, } from "./schema.js";
3
+ async function paginate(rowsQuery, countQuery, limit, offset) {
4
+ const [data, countResult] = await Promise.all([rowsQuery, countQuery]);
5
+ return { data, total: countResult[0]?.count ?? 0, limit, offset };
6
+ }
7
+ export const bookingRequirementsService = {
8
+ async listProductContactRequirements(db, query) {
9
+ const conditions = [];
10
+ if (query.productId)
11
+ conditions.push(eq(productContactRequirements.productId, query.productId));
12
+ if (query.optionId)
13
+ conditions.push(eq(productContactRequirements.optionId, query.optionId));
14
+ if (query.active !== undefined)
15
+ conditions.push(eq(productContactRequirements.active, query.active));
16
+ const where = conditions.length ? and(...conditions) : undefined;
17
+ return paginate(db
18
+ .select()
19
+ .from(productContactRequirements)
20
+ .where(where)
21
+ .limit(query.limit)
22
+ .offset(query.offset)
23
+ .orderBy(asc(productContactRequirements.sortOrder)), db
24
+ .select({ count: sql `count(*)::int` })
25
+ .from(productContactRequirements)
26
+ .where(where), query.limit, query.offset);
27
+ },
28
+ async getProductContactRequirementById(db, id) {
29
+ const [row] = await db
30
+ .select()
31
+ .from(productContactRequirements)
32
+ .where(eq(productContactRequirements.id, id))
33
+ .limit(1);
34
+ return row ?? null;
35
+ },
36
+ async createProductContactRequirement(db, data) {
37
+ const [row] = await db.insert(productContactRequirements).values(data).returning();
38
+ return row ?? null;
39
+ },
40
+ async updateProductContactRequirement(db, id, data) {
41
+ const [row] = await db
42
+ .update(productContactRequirements)
43
+ .set({ ...data, updatedAt: new Date() })
44
+ .where(eq(productContactRequirements.id, id))
45
+ .returning();
46
+ return row ?? null;
47
+ },
48
+ async deleteProductContactRequirement(db, id) {
49
+ const [row] = await db
50
+ .delete(productContactRequirements)
51
+ .where(eq(productContactRequirements.id, id))
52
+ .returning({ id: productContactRequirements.id });
53
+ return row ?? null;
54
+ },
55
+ async listProductBookingQuestions(db, query) {
56
+ const conditions = [];
57
+ if (query.productId)
58
+ conditions.push(eq(productBookingQuestions.productId, query.productId));
59
+ if (query.target)
60
+ conditions.push(eq(productBookingQuestions.target, query.target));
61
+ if (query.fieldType)
62
+ conditions.push(eq(productBookingQuestions.fieldType, query.fieldType));
63
+ if (query.active !== undefined)
64
+ conditions.push(eq(productBookingQuestions.active, query.active));
65
+ const where = conditions.length ? and(...conditions) : undefined;
66
+ return paginate(db
67
+ .select()
68
+ .from(productBookingQuestions)
69
+ .where(where)
70
+ .limit(query.limit)
71
+ .offset(query.offset)
72
+ .orderBy(asc(productBookingQuestions.sortOrder)), db.select({ count: sql `count(*)::int` }).from(productBookingQuestions).where(where), query.limit, query.offset);
73
+ },
74
+ async getProductBookingQuestionById(db, id) {
75
+ const [row] = await db
76
+ .select()
77
+ .from(productBookingQuestions)
78
+ .where(eq(productBookingQuestions.id, id))
79
+ .limit(1);
80
+ return row ?? null;
81
+ },
82
+ async createProductBookingQuestion(db, data) {
83
+ const [row] = await db.insert(productBookingQuestions).values(data).returning();
84
+ return row ?? null;
85
+ },
86
+ async updateProductBookingQuestion(db, id, data) {
87
+ const [row] = await db
88
+ .update(productBookingQuestions)
89
+ .set({ ...data, updatedAt: new Date() })
90
+ .where(eq(productBookingQuestions.id, id))
91
+ .returning();
92
+ return row ?? null;
93
+ },
94
+ async deleteProductBookingQuestion(db, id) {
95
+ const [row] = await db
96
+ .delete(productBookingQuestions)
97
+ .where(eq(productBookingQuestions.id, id))
98
+ .returning({ id: productBookingQuestions.id });
99
+ return row ?? null;
100
+ },
101
+ async listOptionBookingQuestions(db, query) {
102
+ const conditions = [];
103
+ if (query.optionId)
104
+ conditions.push(eq(optionBookingQuestions.optionId, query.optionId));
105
+ if (query.productBookingQuestionId)
106
+ conditions.push(eq(optionBookingQuestions.productBookingQuestionId, query.productBookingQuestionId));
107
+ if (query.active !== undefined)
108
+ conditions.push(eq(optionBookingQuestions.active, query.active));
109
+ const where = conditions.length ? and(...conditions) : undefined;
110
+ return paginate(db
111
+ .select()
112
+ .from(optionBookingQuestions)
113
+ .where(where)
114
+ .limit(query.limit)
115
+ .offset(query.offset)
116
+ .orderBy(asc(optionBookingQuestions.sortOrder)), db.select({ count: sql `count(*)::int` }).from(optionBookingQuestions).where(where), query.limit, query.offset);
117
+ },
118
+ async getOptionBookingQuestionById(db, id) {
119
+ const [row] = await db
120
+ .select()
121
+ .from(optionBookingQuestions)
122
+ .where(eq(optionBookingQuestions.id, id))
123
+ .limit(1);
124
+ return row ?? null;
125
+ },
126
+ async createOptionBookingQuestion(db, data) {
127
+ const [row] = await db.insert(optionBookingQuestions).values(data).returning();
128
+ return row ?? null;
129
+ },
130
+ async updateOptionBookingQuestion(db, id, data) {
131
+ const [row] = await db
132
+ .update(optionBookingQuestions)
133
+ .set({ ...data, updatedAt: new Date() })
134
+ .where(eq(optionBookingQuestions.id, id))
135
+ .returning();
136
+ return row ?? null;
137
+ },
138
+ async deleteOptionBookingQuestion(db, id) {
139
+ const [row] = await db
140
+ .delete(optionBookingQuestions)
141
+ .where(eq(optionBookingQuestions.id, id))
142
+ .returning({ id: optionBookingQuestions.id });
143
+ return row ?? null;
144
+ },
145
+ async listBookingQuestionOptions(db, query) {
146
+ const conditions = [];
147
+ if (query.productBookingQuestionId)
148
+ conditions.push(eq(bookingQuestionOptions.productBookingQuestionId, query.productBookingQuestionId));
149
+ if (query.active !== undefined)
150
+ conditions.push(eq(bookingQuestionOptions.active, query.active));
151
+ const where = conditions.length ? and(...conditions) : undefined;
152
+ return paginate(db
153
+ .select()
154
+ .from(bookingQuestionOptions)
155
+ .where(where)
156
+ .limit(query.limit)
157
+ .offset(query.offset)
158
+ .orderBy(asc(bookingQuestionOptions.sortOrder)), db.select({ count: sql `count(*)::int` }).from(bookingQuestionOptions).where(where), query.limit, query.offset);
159
+ },
160
+ async getBookingQuestionOptionById(db, id) {
161
+ const [row] = await db
162
+ .select()
163
+ .from(bookingQuestionOptions)
164
+ .where(eq(bookingQuestionOptions.id, id))
165
+ .limit(1);
166
+ return row ?? null;
167
+ },
168
+ async createBookingQuestionOption(db, data) {
169
+ const [row] = await db.insert(bookingQuestionOptions).values(data).returning();
170
+ return row ?? null;
171
+ },
172
+ async updateBookingQuestionOption(db, id, data) {
173
+ const [row] = await db
174
+ .update(bookingQuestionOptions)
175
+ .set({ ...data, updatedAt: new Date() })
176
+ .where(eq(bookingQuestionOptions.id, id))
177
+ .returning();
178
+ return row ?? null;
179
+ },
180
+ async deleteBookingQuestionOption(db, id) {
181
+ const [row] = await db
182
+ .delete(bookingQuestionOptions)
183
+ .where(eq(bookingQuestionOptions.id, id))
184
+ .returning({ id: bookingQuestionOptions.id });
185
+ return row ?? null;
186
+ },
187
+ async listBookingQuestionUnitTriggers(db, query) {
188
+ const conditions = [];
189
+ if (query.productBookingQuestionId)
190
+ conditions.push(eq(bookingQuestionUnitTriggers.productBookingQuestionId, query.productBookingQuestionId));
191
+ if (query.unitId)
192
+ conditions.push(eq(bookingQuestionUnitTriggers.unitId, query.unitId));
193
+ if (query.active !== undefined)
194
+ conditions.push(eq(bookingQuestionUnitTriggers.active, query.active));
195
+ const where = conditions.length ? and(...conditions) : undefined;
196
+ return paginate(db
197
+ .select()
198
+ .from(bookingQuestionUnitTriggers)
199
+ .where(where)
200
+ .limit(query.limit)
201
+ .offset(query.offset)
202
+ .orderBy(desc(bookingQuestionUnitTriggers.createdAt)), db
203
+ .select({ count: sql `count(*)::int` })
204
+ .from(bookingQuestionUnitTriggers)
205
+ .where(where), query.limit, query.offset);
206
+ },
207
+ async getBookingQuestionUnitTriggerById(db, id) {
208
+ const [row] = await db
209
+ .select()
210
+ .from(bookingQuestionUnitTriggers)
211
+ .where(eq(bookingQuestionUnitTriggers.id, id))
212
+ .limit(1);
213
+ return row ?? null;
214
+ },
215
+ async createBookingQuestionUnitTrigger(db, data) {
216
+ const [row] = await db.insert(bookingQuestionUnitTriggers).values(data).returning();
217
+ return row ?? null;
218
+ },
219
+ async updateBookingQuestionUnitTrigger(db, id, data) {
220
+ const [row] = await db
221
+ .update(bookingQuestionUnitTriggers)
222
+ .set({ ...data, updatedAt: new Date() })
223
+ .where(eq(bookingQuestionUnitTriggers.id, id))
224
+ .returning();
225
+ return row ?? null;
226
+ },
227
+ async deleteBookingQuestionUnitTrigger(db, id) {
228
+ const [row] = await db
229
+ .delete(bookingQuestionUnitTriggers)
230
+ .where(eq(bookingQuestionUnitTriggers.id, id))
231
+ .returning({ id: bookingQuestionUnitTriggers.id });
232
+ return row ?? null;
233
+ },
234
+ async listBookingQuestionOptionTriggers(db, query) {
235
+ const conditions = [];
236
+ if (query.productBookingQuestionId)
237
+ conditions.push(eq(bookingQuestionOptionTriggers.productBookingQuestionId, query.productBookingQuestionId));
238
+ if (query.optionId)
239
+ conditions.push(eq(bookingQuestionOptionTriggers.optionId, query.optionId));
240
+ if (query.active !== undefined)
241
+ conditions.push(eq(bookingQuestionOptionTriggers.active, query.active));
242
+ const where = conditions.length ? and(...conditions) : undefined;
243
+ return paginate(db
244
+ .select()
245
+ .from(bookingQuestionOptionTriggers)
246
+ .where(where)
247
+ .limit(query.limit)
248
+ .offset(query.offset)
249
+ .orderBy(desc(bookingQuestionOptionTriggers.createdAt)), db
250
+ .select({ count: sql `count(*)::int` })
251
+ .from(bookingQuestionOptionTriggers)
252
+ .where(where), query.limit, query.offset);
253
+ },
254
+ async getBookingQuestionOptionTriggerById(db, id) {
255
+ const [row] = await db
256
+ .select()
257
+ .from(bookingQuestionOptionTriggers)
258
+ .where(eq(bookingQuestionOptionTriggers.id, id))
259
+ .limit(1);
260
+ return row ?? null;
261
+ },
262
+ async createBookingQuestionOptionTrigger(db, data) {
263
+ const [row] = await db.insert(bookingQuestionOptionTriggers).values(data).returning();
264
+ return row ?? null;
265
+ },
266
+ async updateBookingQuestionOptionTrigger(db, id, data) {
267
+ const [row] = await db
268
+ .update(bookingQuestionOptionTriggers)
269
+ .set({ ...data, updatedAt: new Date() })
270
+ .where(eq(bookingQuestionOptionTriggers.id, id))
271
+ .returning();
272
+ return row ?? null;
273
+ },
274
+ async deleteBookingQuestionOptionTrigger(db, id) {
275
+ const [row] = await db
276
+ .delete(bookingQuestionOptionTriggers)
277
+ .where(eq(bookingQuestionOptionTriggers.id, id))
278
+ .returning({ id: bookingQuestionOptionTriggers.id });
279
+ return row ?? null;
280
+ },
281
+ async listBookingQuestionExtraTriggers(db, query) {
282
+ const conditions = [];
283
+ if (query.productBookingQuestionId)
284
+ conditions.push(eq(bookingQuestionExtraTriggers.productBookingQuestionId, query.productBookingQuestionId));
285
+ if (query.productExtraId)
286
+ conditions.push(eq(bookingQuestionExtraTriggers.productExtraId, query.productExtraId));
287
+ if (query.optionExtraConfigId)
288
+ conditions.push(eq(bookingQuestionExtraTriggers.optionExtraConfigId, query.optionExtraConfigId));
289
+ if (query.active !== undefined)
290
+ conditions.push(eq(bookingQuestionExtraTriggers.active, query.active));
291
+ const where = conditions.length ? and(...conditions) : undefined;
292
+ return paginate(db
293
+ .select()
294
+ .from(bookingQuestionExtraTriggers)
295
+ .where(where)
296
+ .limit(query.limit)
297
+ .offset(query.offset)
298
+ .orderBy(desc(bookingQuestionExtraTriggers.createdAt)), db
299
+ .select({ count: sql `count(*)::int` })
300
+ .from(bookingQuestionExtraTriggers)
301
+ .where(where), query.limit, query.offset);
302
+ },
303
+ async getBookingQuestionExtraTriggerById(db, id) {
304
+ const [row] = await db
305
+ .select()
306
+ .from(bookingQuestionExtraTriggers)
307
+ .where(eq(bookingQuestionExtraTriggers.id, id))
308
+ .limit(1);
309
+ return row ?? null;
310
+ },
311
+ async createBookingQuestionExtraTrigger(db, data) {
312
+ const [row] = await db.insert(bookingQuestionExtraTriggers).values(data).returning();
313
+ return row ?? null;
314
+ },
315
+ async updateBookingQuestionExtraTrigger(db, id, data) {
316
+ const [row] = await db
317
+ .update(bookingQuestionExtraTriggers)
318
+ .set({ ...data, updatedAt: new Date() })
319
+ .where(eq(bookingQuestionExtraTriggers.id, id))
320
+ .returning();
321
+ return row ?? null;
322
+ },
323
+ async deleteBookingQuestionExtraTrigger(db, id) {
324
+ const [row] = await db
325
+ .delete(bookingQuestionExtraTriggers)
326
+ .where(eq(bookingQuestionExtraTriggers.id, id))
327
+ .returning({ id: bookingQuestionExtraTriggers.id });
328
+ return row ?? null;
329
+ },
330
+ async listBookingAnswers(db, query) {
331
+ const conditions = [];
332
+ if (query.bookingId)
333
+ conditions.push(eq(bookingAnswers.bookingId, query.bookingId));
334
+ if (query.productBookingQuestionId)
335
+ conditions.push(eq(bookingAnswers.productBookingQuestionId, query.productBookingQuestionId));
336
+ if (query.bookingParticipantId)
337
+ conditions.push(eq(bookingAnswers.bookingParticipantId, query.bookingParticipantId));
338
+ if (query.bookingExtraId)
339
+ conditions.push(eq(bookingAnswers.bookingExtraId, query.bookingExtraId));
340
+ if (query.target)
341
+ conditions.push(eq(bookingAnswers.target, query.target));
342
+ const where = conditions.length ? and(...conditions) : undefined;
343
+ return paginate(db
344
+ .select()
345
+ .from(bookingAnswers)
346
+ .where(where)
347
+ .limit(query.limit)
348
+ .offset(query.offset)
349
+ .orderBy(desc(bookingAnswers.updatedAt)), db.select({ count: sql `count(*)::int` }).from(bookingAnswers).where(where), query.limit, query.offset);
350
+ },
351
+ async getBookingAnswerById(db, id) {
352
+ const [row] = await db.select().from(bookingAnswers).where(eq(bookingAnswers.id, id)).limit(1);
353
+ return row ?? null;
354
+ },
355
+ async createBookingAnswer(db, data) {
356
+ const [row] = await db.insert(bookingAnswers).values(data).returning();
357
+ return row ?? null;
358
+ },
359
+ async updateBookingAnswer(db, id, data) {
360
+ const [row] = await db
361
+ .update(bookingAnswers)
362
+ .set({ ...data, updatedAt: new Date() })
363
+ .where(eq(bookingAnswers.id, id))
364
+ .returning();
365
+ return row ?? null;
366
+ },
367
+ async deleteBookingAnswer(db, id) {
368
+ const [row] = await db
369
+ .delete(bookingAnswers)
370
+ .where(eq(bookingAnswers.id, id))
371
+ .returning({ id: bookingAnswers.id });
372
+ return row ?? null;
373
+ },
374
+ };