@voyantjs/facilities 0.1.1 → 0.3.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.
- package/dist/routes.d.ts +2 -2
- package/dist/service-core.d.ts +109 -0
- package/dist/service-core.d.ts.map +1 -0
- package/dist/service-core.js +152 -0
- package/dist/service-identity.d.ts +168 -0
- package/dist/service-identity.d.ts.map +1 -0
- package/dist/service-identity.js +68 -0
- package/dist/service-operations.d.ts +97 -0
- package/dist/service-operations.d.ts.map +1 -0
- package/dist/service-operations.js +85 -0
- package/dist/service-properties.d.ts +195 -0
- package/dist/service-properties.d.ts.map +1 -0
- package/dist/service-properties.js +157 -0
- package/dist/service-shared.d.ts +63 -0
- package/dist/service-shared.d.ts.map +1 -0
- package/dist/service-shared.js +136 -0
- package/dist/service.d.ts +44 -582
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +44 -599
- package/package.json +5 -5
package/dist/service.js
CHANGED
|
@@ -1,601 +1,46 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
const facilityEntityType = "facility";
|
|
6
|
-
const facilityBaseIdentitySource = "facilities.base";
|
|
7
|
-
const facilityContactIdentitySource = "facilities.contacts";
|
|
8
|
-
async function paginate(rowsQuery, countQuery, limit, offset) {
|
|
9
|
-
const [data, countResult] = await Promise.all([rowsQuery, countQuery]);
|
|
10
|
-
return {
|
|
11
|
-
data,
|
|
12
|
-
total: countResult[0]?.count ?? 0,
|
|
13
|
-
limit,
|
|
14
|
-
offset,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
function isManagedBySource(metadata, source) {
|
|
18
|
-
return metadata?.managedBy === source;
|
|
19
|
-
}
|
|
20
|
-
function toNullableTrimmed(value) {
|
|
21
|
-
const trimmed = value?.trim();
|
|
22
|
-
return trimmed ? trimmed : null;
|
|
23
|
-
}
|
|
24
|
-
function formatAddress(address) {
|
|
25
|
-
if (address.fullText) {
|
|
26
|
-
return address.fullText;
|
|
27
|
-
}
|
|
28
|
-
const parts = [
|
|
29
|
-
address.line1,
|
|
30
|
-
address.line2,
|
|
31
|
-
address.city,
|
|
32
|
-
address.region,
|
|
33
|
-
address.postalCode,
|
|
34
|
-
address.country,
|
|
35
|
-
].filter(Boolean);
|
|
36
|
-
return parts.length > 0 ? parts.join(", ") : null;
|
|
37
|
-
}
|
|
38
|
-
async function syncFacilityAddress(db, facilityId, data) {
|
|
39
|
-
const existingAddresses = await identityService.listAddressesForEntity(db, facilityEntityType, facilityId);
|
|
40
|
-
const managedAddress = existingAddresses.find((address) => isManagedBySource(address.metadata, facilityBaseIdentitySource));
|
|
41
|
-
const line1 = toNullableTrimmed(data.addressLine1);
|
|
42
|
-
const line2 = toNullableTrimmed(data.addressLine2);
|
|
43
|
-
const city = toNullableTrimmed(data.city);
|
|
44
|
-
const region = toNullableTrimmed(data.region);
|
|
45
|
-
const postalCode = toNullableTrimmed(data.postalCode);
|
|
46
|
-
const country = toNullableTrimmed(data.country);
|
|
47
|
-
const hasAddress = Boolean(line1 ||
|
|
48
|
-
line2 ||
|
|
49
|
-
city ||
|
|
50
|
-
region ||
|
|
51
|
-
postalCode ||
|
|
52
|
-
country ||
|
|
53
|
-
data.latitude !== null ||
|
|
54
|
-
data.longitude !== null);
|
|
55
|
-
if (!hasAddress) {
|
|
56
|
-
if (managedAddress) {
|
|
57
|
-
await identityService.deleteAddress(db, managedAddress.id);
|
|
58
|
-
}
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const payload = {
|
|
62
|
-
entityType: facilityEntityType,
|
|
63
|
-
entityId: facilityId,
|
|
64
|
-
label: "primary",
|
|
65
|
-
fullText: null,
|
|
66
|
-
line1,
|
|
67
|
-
line2,
|
|
68
|
-
city,
|
|
69
|
-
region,
|
|
70
|
-
postalCode,
|
|
71
|
-
country,
|
|
72
|
-
latitude: data.latitude ?? null,
|
|
73
|
-
longitude: data.longitude ?? null,
|
|
74
|
-
isPrimary: true,
|
|
75
|
-
metadata: {
|
|
76
|
-
managedBy: facilityBaseIdentitySource,
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
if (managedAddress) {
|
|
80
|
-
await identityService.updateAddress(db, managedAddress.id, payload);
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
await identityService.createAddress(db, payload);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
async function hydrateFacilities(db, rows) {
|
|
87
|
-
if (rows.length === 0) {
|
|
88
|
-
return rows.map((row) => ({
|
|
89
|
-
...row,
|
|
90
|
-
addressLine1: null,
|
|
91
|
-
addressLine2: null,
|
|
92
|
-
city: null,
|
|
93
|
-
region: null,
|
|
94
|
-
country: null,
|
|
95
|
-
postalCode: null,
|
|
96
|
-
latitude: null,
|
|
97
|
-
longitude: null,
|
|
98
|
-
address: null,
|
|
99
|
-
}));
|
|
100
|
-
}
|
|
101
|
-
const ids = rows.map((row) => row.id);
|
|
102
|
-
const addresses = await db
|
|
103
|
-
.select()
|
|
104
|
-
.from(identityAddresses)
|
|
105
|
-
.where(and(eq(identityAddresses.entityType, facilityEntityType), inArray(identityAddresses.entityId, ids)));
|
|
106
|
-
const addressMap = new Map();
|
|
107
|
-
for (const address of addresses) {
|
|
108
|
-
const bucket = addressMap.get(address.entityId) ?? [];
|
|
109
|
-
bucket.push(address);
|
|
110
|
-
addressMap.set(address.entityId, bucket);
|
|
111
|
-
}
|
|
112
|
-
return rows.map((row) => {
|
|
113
|
-
const entityAddresses = addressMap.get(row.id) ?? [];
|
|
114
|
-
const primaryAddress = entityAddresses.find((address) => address.isPrimary) ?? entityAddresses[0] ?? null;
|
|
115
|
-
return {
|
|
116
|
-
...row,
|
|
117
|
-
addressLine1: primaryAddress?.line1 ?? null,
|
|
118
|
-
addressLine2: primaryAddress?.line2 ?? null,
|
|
119
|
-
city: primaryAddress?.city ?? null,
|
|
120
|
-
region: primaryAddress?.region ?? null,
|
|
121
|
-
country: primaryAddress?.country ?? null,
|
|
122
|
-
postalCode: primaryAddress?.postalCode ?? null,
|
|
123
|
-
latitude: primaryAddress?.latitude ?? null,
|
|
124
|
-
longitude: primaryAddress?.longitude ?? null,
|
|
125
|
-
address: primaryAddress ? formatAddress(primaryAddress) : null,
|
|
126
|
-
};
|
|
127
|
-
});
|
|
128
|
-
}
|
|
1
|
+
import { createFacility, deleteFacility, getFacilityById, listFacilities, updateFacility, } from "./service-core.js";
|
|
2
|
+
import { createAddress, createContactPoint, createFacilityContact, deleteAddress, deleteContactPoint, deleteFacilityContact, listAddresses, listContactPoints, listFacilityContacts, updateAddress, updateContactPoint, updateFacilityContact, } from "./service-identity.js";
|
|
3
|
+
import { createFacilityFeature, createFacilityOperationSchedule, deleteFacilityFeature, deleteFacilityOperationSchedule, listFacilityFeatures, listFacilityOperationSchedules, updateFacilityFeature, updateFacilityOperationSchedule, } from "./service-operations.js";
|
|
4
|
+
import { createProperty, createPropertyGroup, createPropertyGroupMember, deleteProperty, deletePropertyGroup, deletePropertyGroupMember, getPropertyById, getPropertyGroupById, getPropertyGroupMemberById, listProperties, listPropertyGroupMembers, listPropertyGroups, updateProperty, updatePropertyGroup, updatePropertyGroupMember, } from "./service-properties.js";
|
|
129
5
|
export const facilitiesService = {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
|
172
|
-
const page = await paginate(db
|
|
173
|
-
.select()
|
|
174
|
-
.from(facilities)
|
|
175
|
-
.where(where)
|
|
176
|
-
.limit(query.limit)
|
|
177
|
-
.offset(query.offset)
|
|
178
|
-
.orderBy(desc(facilities.updatedAt)), db.select({ count: sql `count(*)::int` }).from(facilities).where(where), query.limit, query.offset);
|
|
179
|
-
return {
|
|
180
|
-
...page,
|
|
181
|
-
data: await hydrateFacilities(db, page.data),
|
|
182
|
-
};
|
|
183
|
-
},
|
|
184
|
-
async getFacilityById(db, id) {
|
|
185
|
-
const [row] = await db.select().from(facilities).where(eq(facilities.id, id)).limit(1);
|
|
186
|
-
if (!row)
|
|
187
|
-
return null;
|
|
188
|
-
const [hydrated] = await hydrateFacilities(db, [row]);
|
|
189
|
-
return hydrated ?? null;
|
|
190
|
-
},
|
|
191
|
-
async createFacility(db, data) {
|
|
192
|
-
const { addressLine1, addressLine2, city, region, postalCode, country, latitude, longitude, ...facilityValues } = data;
|
|
193
|
-
const [row] = await db.insert(facilities).values(facilityValues).returning();
|
|
194
|
-
if (!row) {
|
|
195
|
-
throw new Error("Failed to create facility");
|
|
196
|
-
}
|
|
197
|
-
await syncFacilityAddress(db, row.id, {
|
|
198
|
-
addressLine1,
|
|
199
|
-
addressLine2,
|
|
200
|
-
city,
|
|
201
|
-
region,
|
|
202
|
-
postalCode,
|
|
203
|
-
country,
|
|
204
|
-
latitude,
|
|
205
|
-
longitude,
|
|
206
|
-
});
|
|
207
|
-
return {
|
|
208
|
-
...row,
|
|
209
|
-
addressLine1: addressLine1 ?? null,
|
|
210
|
-
addressLine2: addressLine2 ?? null,
|
|
211
|
-
city: city ?? null,
|
|
212
|
-
region: region ?? null,
|
|
213
|
-
postalCode: postalCode ?? null,
|
|
214
|
-
country: country ?? null,
|
|
215
|
-
latitude: latitude ?? null,
|
|
216
|
-
longitude: longitude ?? null,
|
|
217
|
-
address: formatAddress({
|
|
218
|
-
fullText: null,
|
|
219
|
-
line1: addressLine1 ?? null,
|
|
220
|
-
line2: addressLine2 ?? null,
|
|
221
|
-
city: city ?? null,
|
|
222
|
-
region: region ?? null,
|
|
223
|
-
postalCode: postalCode ?? null,
|
|
224
|
-
country: country ?? null,
|
|
225
|
-
}),
|
|
226
|
-
};
|
|
227
|
-
},
|
|
228
|
-
async updateFacility(db, id, data) {
|
|
229
|
-
const existing = await this.getFacilityById(db, id);
|
|
230
|
-
if (!existing)
|
|
231
|
-
return null;
|
|
232
|
-
const { addressLine1, addressLine2, city, region, postalCode, country, latitude, longitude, ...facilityValues } = data;
|
|
233
|
-
const [row] = await db
|
|
234
|
-
.update(facilities)
|
|
235
|
-
.set({ ...facilityValues, updatedAt: new Date() })
|
|
236
|
-
.where(eq(facilities.id, id))
|
|
237
|
-
.returning();
|
|
238
|
-
if (!row)
|
|
239
|
-
return null;
|
|
240
|
-
await syncFacilityAddress(db, id, {
|
|
241
|
-
addressLine1: addressLine1 ?? existing.addressLine1,
|
|
242
|
-
addressLine2: addressLine2 ?? existing.addressLine2,
|
|
243
|
-
city: city ?? existing.city,
|
|
244
|
-
region: region ?? existing.region,
|
|
245
|
-
postalCode: postalCode ?? existing.postalCode,
|
|
246
|
-
country: country ?? existing.country,
|
|
247
|
-
latitude: latitude ?? existing.latitude,
|
|
248
|
-
longitude: longitude ?? existing.longitude,
|
|
249
|
-
});
|
|
250
|
-
return {
|
|
251
|
-
...row,
|
|
252
|
-
addressLine1: addressLine1 ?? existing.addressLine1,
|
|
253
|
-
addressLine2: addressLine2 ?? existing.addressLine2,
|
|
254
|
-
city: city ?? existing.city,
|
|
255
|
-
region: region ?? existing.region,
|
|
256
|
-
postalCode: postalCode ?? existing.postalCode,
|
|
257
|
-
country: country ?? existing.country,
|
|
258
|
-
latitude: latitude ?? existing.latitude,
|
|
259
|
-
longitude: longitude ?? existing.longitude,
|
|
260
|
-
address: formatAddress({
|
|
261
|
-
fullText: null,
|
|
262
|
-
line1: addressLine1 ?? existing.addressLine1,
|
|
263
|
-
line2: addressLine2 ?? existing.addressLine2,
|
|
264
|
-
city: city ?? existing.city,
|
|
265
|
-
region: region ?? existing.region,
|
|
266
|
-
postalCode: postalCode ?? existing.postalCode,
|
|
267
|
-
country: country ?? existing.country,
|
|
268
|
-
}),
|
|
269
|
-
};
|
|
270
|
-
},
|
|
271
|
-
async deleteFacility(db, id) {
|
|
272
|
-
const [row] = await db
|
|
273
|
-
.delete(facilities)
|
|
274
|
-
.where(eq(facilities.id, id))
|
|
275
|
-
.returning({ id: facilities.id });
|
|
276
|
-
return row ?? null;
|
|
277
|
-
},
|
|
278
|
-
listContactPoints(db, facilityId) {
|
|
279
|
-
return identityService.listContactPointsForEntity(db, facilityEntityType, facilityId);
|
|
280
|
-
},
|
|
281
|
-
async createContactPoint(db, facilityId, data) {
|
|
282
|
-
const [facility] = await db
|
|
283
|
-
.select({ id: facilities.id })
|
|
284
|
-
.from(facilities)
|
|
285
|
-
.where(eq(facilities.id, facilityId))
|
|
286
|
-
.limit(1);
|
|
287
|
-
if (!facility)
|
|
288
|
-
return null;
|
|
289
|
-
return identityService.createContactPoint(db, {
|
|
290
|
-
...data,
|
|
291
|
-
entityType: facilityEntityType,
|
|
292
|
-
entityId: facilityId,
|
|
293
|
-
});
|
|
294
|
-
},
|
|
295
|
-
updateContactPoint(db, id, data) {
|
|
296
|
-
return identityService.updateContactPoint(db, id, data);
|
|
297
|
-
},
|
|
298
|
-
deleteContactPoint(db, id) {
|
|
299
|
-
return identityService.deleteContactPoint(db, id);
|
|
300
|
-
},
|
|
301
|
-
listAddresses(db, facilityId) {
|
|
302
|
-
return identityService.listAddressesForEntity(db, facilityEntityType, facilityId);
|
|
303
|
-
},
|
|
304
|
-
async createAddress(db, facilityId, data) {
|
|
305
|
-
const [facility] = await db
|
|
306
|
-
.select({ id: facilities.id })
|
|
307
|
-
.from(facilities)
|
|
308
|
-
.where(eq(facilities.id, facilityId))
|
|
309
|
-
.limit(1);
|
|
310
|
-
if (!facility)
|
|
311
|
-
return null;
|
|
312
|
-
return identityService.createAddress(db, {
|
|
313
|
-
...data,
|
|
314
|
-
entityType: facilityEntityType,
|
|
315
|
-
entityId: facilityId,
|
|
316
|
-
});
|
|
317
|
-
},
|
|
318
|
-
updateAddress(db, id, data) {
|
|
319
|
-
return identityService.updateAddress(db, id, data);
|
|
320
|
-
},
|
|
321
|
-
deleteAddress(db, id) {
|
|
322
|
-
return identityService.deleteAddress(db, id);
|
|
323
|
-
},
|
|
324
|
-
async listFacilityContacts(db, query) {
|
|
325
|
-
return identityService.listNamedContacts(db, {
|
|
326
|
-
entityType: facilityEntityType,
|
|
327
|
-
entityId: query.facilityId,
|
|
328
|
-
role: query.role,
|
|
329
|
-
limit: query.limit,
|
|
330
|
-
offset: query.offset,
|
|
331
|
-
});
|
|
332
|
-
},
|
|
333
|
-
async createFacilityContact(db, facilityId, data) {
|
|
334
|
-
const [facility] = await db
|
|
335
|
-
.select({ id: facilities.id })
|
|
336
|
-
.from(facilities)
|
|
337
|
-
.where(eq(facilities.id, facilityId))
|
|
338
|
-
.limit(1);
|
|
339
|
-
if (!facility)
|
|
340
|
-
return null;
|
|
341
|
-
return identityService.createNamedContact(db, {
|
|
342
|
-
...data,
|
|
343
|
-
entityType: facilityEntityType,
|
|
344
|
-
entityId: facilityId,
|
|
345
|
-
metadata: {
|
|
346
|
-
managedBy: facilityContactIdentitySource,
|
|
347
|
-
},
|
|
348
|
-
});
|
|
349
|
-
},
|
|
350
|
-
async updateFacilityContact(db, id, data) {
|
|
351
|
-
return identityService.updateNamedContact(db, id, data);
|
|
352
|
-
},
|
|
353
|
-
async deleteFacilityContact(db, id) {
|
|
354
|
-
return identityService.deleteNamedContact(db, id);
|
|
355
|
-
},
|
|
356
|
-
async listFacilityFeatures(db, query) {
|
|
357
|
-
const conditions = [];
|
|
358
|
-
if (query.facilityId)
|
|
359
|
-
conditions.push(eq(facilityFeatures.facilityId, query.facilityId));
|
|
360
|
-
if (query.category)
|
|
361
|
-
conditions.push(eq(facilityFeatures.category, query.category));
|
|
362
|
-
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
|
363
|
-
return paginate(db
|
|
364
|
-
.select()
|
|
365
|
-
.from(facilityFeatures)
|
|
366
|
-
.where(where)
|
|
367
|
-
.limit(query.limit)
|
|
368
|
-
.offset(query.offset)
|
|
369
|
-
.orderBy(facilityFeatures.sortOrder, facilityFeatures.name), db.select({ count: sql `count(*)::int` }).from(facilityFeatures).where(where), query.limit, query.offset);
|
|
370
|
-
},
|
|
371
|
-
async createFacilityFeature(db, facilityId, data) {
|
|
372
|
-
const [facility] = await db
|
|
373
|
-
.select({ id: facilities.id })
|
|
374
|
-
.from(facilities)
|
|
375
|
-
.where(eq(facilities.id, facilityId))
|
|
376
|
-
.limit(1);
|
|
377
|
-
if (!facility)
|
|
378
|
-
return null;
|
|
379
|
-
const [row] = await db
|
|
380
|
-
.insert(facilityFeatures)
|
|
381
|
-
.values({ ...data, facilityId })
|
|
382
|
-
.returning();
|
|
383
|
-
return row ?? null;
|
|
384
|
-
},
|
|
385
|
-
async updateFacilityFeature(db, id, data) {
|
|
386
|
-
const [row] = await db
|
|
387
|
-
.update(facilityFeatures)
|
|
388
|
-
.set({ ...data, updatedAt: new Date() })
|
|
389
|
-
.where(eq(facilityFeatures.id, id))
|
|
390
|
-
.returning();
|
|
391
|
-
return row ?? null;
|
|
392
|
-
},
|
|
393
|
-
async deleteFacilityFeature(db, id) {
|
|
394
|
-
const [row] = await db
|
|
395
|
-
.delete(facilityFeatures)
|
|
396
|
-
.where(eq(facilityFeatures.id, id))
|
|
397
|
-
.returning({ id: facilityFeatures.id });
|
|
398
|
-
return row ?? null;
|
|
399
|
-
},
|
|
400
|
-
async listFacilityOperationSchedules(db, query) {
|
|
401
|
-
const conditions = [];
|
|
402
|
-
if (query.facilityId)
|
|
403
|
-
conditions.push(eq(facilityOperationSchedules.facilityId, query.facilityId));
|
|
404
|
-
if (query.dayOfWeek)
|
|
405
|
-
conditions.push(eq(facilityOperationSchedules.dayOfWeek, query.dayOfWeek));
|
|
406
|
-
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
|
407
|
-
return paginate(db
|
|
408
|
-
.select()
|
|
409
|
-
.from(facilityOperationSchedules)
|
|
410
|
-
.where(where)
|
|
411
|
-
.limit(query.limit)
|
|
412
|
-
.offset(query.offset)
|
|
413
|
-
.orderBy(facilityOperationSchedules.dayOfWeek, desc(facilityOperationSchedules.validFrom)), db
|
|
414
|
-
.select({ count: sql `count(*)::int` })
|
|
415
|
-
.from(facilityOperationSchedules)
|
|
416
|
-
.where(where), query.limit, query.offset);
|
|
417
|
-
},
|
|
418
|
-
async createFacilityOperationSchedule(db, facilityId, data) {
|
|
419
|
-
const [facility] = await db
|
|
420
|
-
.select({ id: facilities.id })
|
|
421
|
-
.from(facilities)
|
|
422
|
-
.where(eq(facilities.id, facilityId))
|
|
423
|
-
.limit(1);
|
|
424
|
-
if (!facility)
|
|
425
|
-
return null;
|
|
426
|
-
const [row] = await db
|
|
427
|
-
.insert(facilityOperationSchedules)
|
|
428
|
-
.values({ ...data, facilityId })
|
|
429
|
-
.returning();
|
|
430
|
-
return row ?? null;
|
|
431
|
-
},
|
|
432
|
-
async updateFacilityOperationSchedule(db, id, data) {
|
|
433
|
-
const [row] = await db
|
|
434
|
-
.update(facilityOperationSchedules)
|
|
435
|
-
.set({ ...data, updatedAt: new Date() })
|
|
436
|
-
.where(eq(facilityOperationSchedules.id, id))
|
|
437
|
-
.returning();
|
|
438
|
-
return row ?? null;
|
|
439
|
-
},
|
|
440
|
-
async deleteFacilityOperationSchedule(db, id) {
|
|
441
|
-
const [row] = await db
|
|
442
|
-
.delete(facilityOperationSchedules)
|
|
443
|
-
.where(eq(facilityOperationSchedules.id, id))
|
|
444
|
-
.returning({ id: facilityOperationSchedules.id });
|
|
445
|
-
return row ?? null;
|
|
446
|
-
},
|
|
447
|
-
async listProperties(db, query) {
|
|
448
|
-
const conditions = [];
|
|
449
|
-
if (query.facilityId)
|
|
450
|
-
conditions.push(eq(properties.facilityId, query.facilityId));
|
|
451
|
-
if (query.propertyType)
|
|
452
|
-
conditions.push(eq(properties.propertyType, query.propertyType));
|
|
453
|
-
if (query.groupName)
|
|
454
|
-
conditions.push(eq(properties.groupName, query.groupName));
|
|
455
|
-
if (query.search) {
|
|
456
|
-
const term = `%${query.search}%`;
|
|
457
|
-
conditions.push(or(ilike(properties.brandName, term), ilike(properties.groupName, term)));
|
|
458
|
-
}
|
|
459
|
-
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
|
460
|
-
return paginate(db
|
|
461
|
-
.select()
|
|
462
|
-
.from(properties)
|
|
463
|
-
.where(where)
|
|
464
|
-
.limit(query.limit)
|
|
465
|
-
.offset(query.offset)
|
|
466
|
-
.orderBy(desc(properties.updatedAt)), db.select({ count: sql `count(*)::int` }).from(properties).where(where), query.limit, query.offset);
|
|
467
|
-
},
|
|
468
|
-
async getPropertyById(db, id) {
|
|
469
|
-
const [row] = await db.select().from(properties).where(eq(properties.id, id)).limit(1);
|
|
470
|
-
return row ?? null;
|
|
471
|
-
},
|
|
472
|
-
async createProperty(db, data) {
|
|
473
|
-
const [facility] = await db
|
|
474
|
-
.select({ id: facilities.id })
|
|
475
|
-
.from(facilities)
|
|
476
|
-
.where(eq(facilities.id, data.facilityId))
|
|
477
|
-
.limit(1);
|
|
478
|
-
if (!facility)
|
|
479
|
-
return null;
|
|
480
|
-
const [row] = await db.insert(properties).values(data).returning();
|
|
481
|
-
return row ?? null;
|
|
482
|
-
},
|
|
483
|
-
async updateProperty(db, id, data) {
|
|
484
|
-
const [row] = await db
|
|
485
|
-
.update(properties)
|
|
486
|
-
.set({ ...data, updatedAt: new Date() })
|
|
487
|
-
.where(eq(properties.id, id))
|
|
488
|
-
.returning();
|
|
489
|
-
return row ?? null;
|
|
490
|
-
},
|
|
491
|
-
async deleteProperty(db, id) {
|
|
492
|
-
const [row] = await db
|
|
493
|
-
.delete(properties)
|
|
494
|
-
.where(eq(properties.id, id))
|
|
495
|
-
.returning({ id: properties.id });
|
|
496
|
-
return row ?? null;
|
|
497
|
-
},
|
|
498
|
-
async listPropertyGroups(db, query) {
|
|
499
|
-
const conditions = [];
|
|
500
|
-
if (query.parentGroupId)
|
|
501
|
-
conditions.push(eq(propertyGroups.parentGroupId, query.parentGroupId));
|
|
502
|
-
if (query.groupType)
|
|
503
|
-
conditions.push(eq(propertyGroups.groupType, query.groupType));
|
|
504
|
-
if (query.status)
|
|
505
|
-
conditions.push(eq(propertyGroups.status, query.status));
|
|
506
|
-
if (query.search) {
|
|
507
|
-
const term = `%${query.search}%`;
|
|
508
|
-
conditions.push(or(ilike(propertyGroups.name, term), ilike(propertyGroups.code, term), ilike(propertyGroups.brandName, term), ilike(propertyGroups.legalName, term)));
|
|
509
|
-
}
|
|
510
|
-
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
|
511
|
-
return paginate(db
|
|
512
|
-
.select()
|
|
513
|
-
.from(propertyGroups)
|
|
514
|
-
.where(where)
|
|
515
|
-
.limit(query.limit)
|
|
516
|
-
.offset(query.offset)
|
|
517
|
-
.orderBy(desc(propertyGroups.updatedAt)), db.select({ count: sql `count(*)::int` }).from(propertyGroups).where(where), query.limit, query.offset);
|
|
518
|
-
},
|
|
519
|
-
async getPropertyGroupById(db, id) {
|
|
520
|
-
const [row] = await db.select().from(propertyGroups).where(eq(propertyGroups.id, id)).limit(1);
|
|
521
|
-
return row ?? null;
|
|
522
|
-
},
|
|
523
|
-
async createPropertyGroup(db, data) {
|
|
524
|
-
const [row] = await db.insert(propertyGroups).values(data).returning();
|
|
525
|
-
return row ?? null;
|
|
526
|
-
},
|
|
527
|
-
async updatePropertyGroup(db, id, data) {
|
|
528
|
-
const [row] = await db
|
|
529
|
-
.update(propertyGroups)
|
|
530
|
-
.set({ ...data, updatedAt: new Date() })
|
|
531
|
-
.where(eq(propertyGroups.id, id))
|
|
532
|
-
.returning();
|
|
533
|
-
return row ?? null;
|
|
534
|
-
},
|
|
535
|
-
async deletePropertyGroup(db, id) {
|
|
536
|
-
const [row] = await db
|
|
537
|
-
.delete(propertyGroups)
|
|
538
|
-
.where(eq(propertyGroups.id, id))
|
|
539
|
-
.returning({ id: propertyGroups.id });
|
|
540
|
-
return row ?? null;
|
|
541
|
-
},
|
|
542
|
-
async listPropertyGroupMembers(db, query) {
|
|
543
|
-
const conditions = [];
|
|
544
|
-
if (query.groupId)
|
|
545
|
-
conditions.push(eq(propertyGroupMembers.groupId, query.groupId));
|
|
546
|
-
if (query.propertyId)
|
|
547
|
-
conditions.push(eq(propertyGroupMembers.propertyId, query.propertyId));
|
|
548
|
-
if (query.membershipRole) {
|
|
549
|
-
conditions.push(eq(propertyGroupMembers.membershipRole, query.membershipRole));
|
|
550
|
-
}
|
|
551
|
-
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
|
552
|
-
return paginate(db
|
|
553
|
-
.select()
|
|
554
|
-
.from(propertyGroupMembers)
|
|
555
|
-
.where(where)
|
|
556
|
-
.limit(query.limit)
|
|
557
|
-
.offset(query.offset)
|
|
558
|
-
.orderBy(desc(propertyGroupMembers.updatedAt)), db.select({ count: sql `count(*)::int` }).from(propertyGroupMembers).where(where), query.limit, query.offset);
|
|
559
|
-
},
|
|
560
|
-
async getPropertyGroupMemberById(db, id) {
|
|
561
|
-
const [row] = await db
|
|
562
|
-
.select()
|
|
563
|
-
.from(propertyGroupMembers)
|
|
564
|
-
.where(eq(propertyGroupMembers.id, id))
|
|
565
|
-
.limit(1);
|
|
566
|
-
return row ?? null;
|
|
567
|
-
},
|
|
568
|
-
async createPropertyGroupMember(db, data) {
|
|
569
|
-
const [group, property] = await Promise.all([
|
|
570
|
-
db
|
|
571
|
-
.select({ id: propertyGroups.id })
|
|
572
|
-
.from(propertyGroups)
|
|
573
|
-
.where(eq(propertyGroups.id, data.groupId))
|
|
574
|
-
.limit(1),
|
|
575
|
-
db
|
|
576
|
-
.select({ id: properties.id })
|
|
577
|
-
.from(properties)
|
|
578
|
-
.where(eq(properties.id, data.propertyId))
|
|
579
|
-
.limit(1),
|
|
580
|
-
]);
|
|
581
|
-
if (!group[0] || !property[0])
|
|
582
|
-
return null;
|
|
583
|
-
const [row] = await db.insert(propertyGroupMembers).values(data).returning();
|
|
584
|
-
return row ?? null;
|
|
585
|
-
},
|
|
586
|
-
async updatePropertyGroupMember(db, id, data) {
|
|
587
|
-
const [row] = await db
|
|
588
|
-
.update(propertyGroupMembers)
|
|
589
|
-
.set({ ...data, updatedAt: new Date() })
|
|
590
|
-
.where(eq(propertyGroupMembers.id, id))
|
|
591
|
-
.returning();
|
|
592
|
-
return row ?? null;
|
|
593
|
-
},
|
|
594
|
-
async deletePropertyGroupMember(db, id) {
|
|
595
|
-
const [row] = await db
|
|
596
|
-
.delete(propertyGroupMembers)
|
|
597
|
-
.where(eq(propertyGroupMembers.id, id))
|
|
598
|
-
.returning({ id: propertyGroupMembers.id });
|
|
599
|
-
return row ?? null;
|
|
600
|
-
},
|
|
6
|
+
listFacilities,
|
|
7
|
+
getFacilityById,
|
|
8
|
+
createFacility,
|
|
9
|
+
updateFacility,
|
|
10
|
+
deleteFacility,
|
|
11
|
+
listContactPoints,
|
|
12
|
+
createContactPoint,
|
|
13
|
+
updateContactPoint,
|
|
14
|
+
deleteContactPoint,
|
|
15
|
+
listAddresses,
|
|
16
|
+
createAddress,
|
|
17
|
+
updateAddress,
|
|
18
|
+
deleteAddress,
|
|
19
|
+
listFacilityContacts,
|
|
20
|
+
createFacilityContact,
|
|
21
|
+
updateFacilityContact,
|
|
22
|
+
deleteFacilityContact,
|
|
23
|
+
listFacilityFeatures,
|
|
24
|
+
createFacilityFeature,
|
|
25
|
+
updateFacilityFeature,
|
|
26
|
+
deleteFacilityFeature,
|
|
27
|
+
listFacilityOperationSchedules,
|
|
28
|
+
createFacilityOperationSchedule,
|
|
29
|
+
updateFacilityOperationSchedule,
|
|
30
|
+
deleteFacilityOperationSchedule,
|
|
31
|
+
listProperties,
|
|
32
|
+
getPropertyById,
|
|
33
|
+
createProperty,
|
|
34
|
+
updateProperty,
|
|
35
|
+
deleteProperty,
|
|
36
|
+
listPropertyGroups,
|
|
37
|
+
getPropertyGroupById,
|
|
38
|
+
createPropertyGroup,
|
|
39
|
+
updatePropertyGroup,
|
|
40
|
+
deletePropertyGroup,
|
|
41
|
+
listPropertyGroupMembers,
|
|
42
|
+
getPropertyGroupMemberById,
|
|
43
|
+
createPropertyGroupMember,
|
|
44
|
+
updatePropertyGroupMember,
|
|
45
|
+
deletePropertyGroupMember,
|
|
601
46
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@voyantjs/facilities",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "FSL-1.1-Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
"drizzle-orm": "^0.45.2",
|
|
26
26
|
"hono": "^4.12.10",
|
|
27
27
|
"zod": "^4.3.6",
|
|
28
|
-
"@voyantjs/core": "0.
|
|
29
|
-
"@voyantjs/db": "0.
|
|
30
|
-
"@voyantjs/hono": "0.
|
|
31
|
-
"@voyantjs/identity": "0.
|
|
28
|
+
"@voyantjs/core": "0.3.0",
|
|
29
|
+
"@voyantjs/db": "0.3.0",
|
|
30
|
+
"@voyantjs/hono": "0.3.0",
|
|
31
|
+
"@voyantjs/identity": "0.3.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"typescript": "^6.0.2",
|