@talkpilot/core-db 1.3.17 → 1.3.18

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.
Files changed (79) hide show
  1. package/README.md +166 -58
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +1 -0
  5. package/dist/index.js.map +1 -1
  6. package/dist/municipal/index.d.ts +1 -1
  7. package/dist/municipal/index.d.ts.map +1 -1
  8. package/dist/municipal/tickets/tickets.statistics.getters.d.ts.map +1 -1
  9. package/dist/municipal/tickets/tickets.statistics.getters.js +6 -2
  10. package/dist/municipal/tickets/tickets.statistics.getters.js.map +1 -1
  11. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +3 -0
  12. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
  13. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +17 -1
  14. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
  15. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +1 -2
  16. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -1
  17. package/dist/talkpilot/flows/flows.schema.d.ts +3 -0
  18. package/dist/talkpilot/flows/flows.schema.d.ts.map +1 -1
  19. package/dist/talkpilot/flows/flows.schema.js +1 -0
  20. package/dist/talkpilot/flows/flows.schema.js.map +1 -1
  21. package/dist/talkpilot/flows/flows.types.d.ts +2 -0
  22. package/dist/talkpilot/flows/flows.types.d.ts.map +1 -1
  23. package/dist/test-utils/factories/index.d.ts +1 -0
  24. package/dist/test-utils/factories/index.d.ts.map +1 -1
  25. package/dist/test-utils/factories/index.js +1 -0
  26. package/dist/test-utils/factories/index.js.map +1 -1
  27. package/dist/test-utils/factories/websitalk/scans.d.ts.map +1 -1
  28. package/dist/test-utils/factories/websitalk/scans.js +2 -1
  29. package/dist/test-utils/factories/websitalk/scans.js.map +1 -1
  30. package/dist/websitalk/index.d.ts +1 -0
  31. package/dist/websitalk/index.d.ts.map +1 -1
  32. package/dist/websitalk/index.js +1 -0
  33. package/dist/websitalk/index.js.map +1 -1
  34. package/dist/websitalk/scans/index.d.ts +2 -0
  35. package/dist/websitalk/scans/index.d.ts.map +1 -1
  36. package/dist/websitalk/scans/index.js +2 -0
  37. package/dist/websitalk/scans/index.js.map +1 -1
  38. package/dist/websitalk/scans/scans.getters.d.ts +10 -3
  39. package/dist/websitalk/scans/scans.getters.d.ts.map +1 -1
  40. package/dist/websitalk/scans/scans.getters.js +49 -9
  41. package/dist/websitalk/scans/scans.getters.js.map +1 -1
  42. package/dist/websitalk/scans/scans.types.d.ts +3 -8
  43. package/dist/websitalk/scans/scans.types.d.ts.map +1 -1
  44. package/package.json +1 -1
  45. package/src/index.ts +1 -0
  46. package/src/municipal/index.ts +1 -1
  47. package/src/municipal/tickets/tickets.statistics.getters.ts +16 -3
  48. package/src/talkpilot/calls/__tests__/calls.statistics.spec.ts +6 -4
  49. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.getters.spec.ts +57 -5
  50. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +0 -4
  51. package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +24 -0
  52. package/src/talkpilot/clientsConfig/clientsConfig.types.ts +1 -2
  53. package/src/talkpilot/flows/__tests__/flows.schema.spec.ts +3 -0
  54. package/src/talkpilot/flows/flows.schema.ts +1 -0
  55. package/src/talkpilot/flows/flows.types.ts +2 -0
  56. package/src/test-utils/factories/index.ts +1 -0
  57. package/src/test-utils/factories/websitalk/scans.ts +2 -1
  58. package/src/test-utils/factories/websitalk/websiteUrls.ts +17 -0
  59. package/src/websitalk/index.ts +1 -0
  60. package/src/websitalk/scans/__tests__/scans.spec.ts +121 -26
  61. package/src/websitalk/scans/index.ts +2 -0
  62. package/src/websitalk/scans/scans.constants.ts +15 -0
  63. package/src/websitalk/scans/scans.getters.ts +64 -17
  64. package/src/websitalk/scans/scans.types.ts +3 -11
  65. package/src/websitalk/scans/scans.utils.ts +5 -0
  66. package/src/websitalk/websiteUrls/__tests__/websiteUrls.spec.ts +77 -0
  67. package/src/websitalk/websiteUrls/index.ts +2 -0
  68. package/src/websitalk/websiteUrls/websiteUrls.getters.ts +65 -0
  69. package/src/websitalk/websiteUrls/websiteUrls.types.ts +13 -0
  70. package/tsconfig.json +4 -4
  71. package/README_OLD.md +0 -160
  72. package/dist/websitalk/websitalk.setup.d.ts +0 -3
  73. package/dist/websitalk/websitalk.setup.d.ts.map +0 -1
  74. package/dist/websitalk/websitalk.setup.js +0 -34
  75. package/dist/websitalk/websitalk.setup.js.map +0 -1
  76. package/dist/websitalk/websiteUrls/websiteUrls.constants.d.ts +0 -2
  77. package/dist/websitalk/websiteUrls/websiteUrls.constants.d.ts.map +0 -1
  78. package/dist/websitalk/websiteUrls/websiteUrls.constants.js +0 -5
  79. package/dist/websitalk/websiteUrls/websiteUrls.constants.js.map +0 -1
@@ -6,7 +6,12 @@ import {
6
6
  countScans,
7
7
  createScanDoc,
8
8
  updateScanDoc,
9
- getActiveScanStatus,
9
+ findInProgressScansByClient,
10
+ getScansByWebsiteUrl,
11
+ getLatestScanByWebsiteUrl,
12
+ getInProgressScanByWebsiteUrl,
13
+ getLatestCompletedScanByWebsiteUrl,
14
+ deleteScansByWebsiteUrl,
10
15
  } from "../scans.getters";
11
16
  import { createScanFixture } from "../../../test-utils/factories";
12
17
  import { ObjectId } from "mongodb";
@@ -37,7 +42,7 @@ describe("db.websitalk.scans", () => {
37
42
  expect(result.length).toBe(1);
38
43
  expect(result[0]).toMatchObject({
39
44
  clientId,
40
- baseUrl: scanInside.baseUrl,
45
+ websiteUrlId: scanInside.websiteUrlId,
41
46
  status: "COMPLETED",
42
47
  });
43
48
  expect(result[0]).toHaveProperty("siteChrome");
@@ -83,12 +88,12 @@ describe("db.websitalk.scans", () => {
83
88
  clientId,
84
89
  new Date("2023-05-01"),
85
90
  new Date("2023-05-31"),
86
- ["clientId", "baseUrl", "status"],
91
+ ["clientId", "websiteUrlId", "status"],
87
92
  );
88
93
 
89
94
  expect(result).toMatchObject({
90
95
  clientId,
91
- baseUrl: scan.baseUrl,
96
+ websiteUrlId: scan.websiteUrlId,
92
97
  status: "COMPLETED",
93
98
  });
94
99
  expect(result).toHaveProperty("_id");
@@ -110,7 +115,7 @@ describe("db.websitalk.scans", () => {
110
115
  const count = await countScans({ clientId });
111
116
 
112
117
  expect(result.length).toBe(1);
113
- expect(result[0].baseUrl).toBe(completed.baseUrl);
118
+ expect(result[0].websiteUrlId).toBe(completed.websiteUrlId);
114
119
  expect(count).toBe(2);
115
120
  });
116
121
  });
@@ -118,15 +123,15 @@ describe("db.websitalk.scans", () => {
118
123
  describe("createScanDoc()", () => {
119
124
  it("should insert a scan with default values for the rest of the fields", async () => {
120
125
  const clientId = "createClient";
121
- const baseUrl = "https://example.com";
126
+ const websiteUrlId = new ObjectId().toString();
122
127
 
123
- const { insertedId } = await createScanDoc({ clientId, baseUrl });
128
+ const { insertedId } = await createScanDoc({ clientId, websiteUrlId });
124
129
 
125
130
  const result = await getScansCollection().findOne({ _id: insertedId });
126
131
 
127
132
  expect(result).toMatchObject({
128
133
  clientId,
129
- baseUrl,
134
+ websiteUrlId,
130
135
  status: "CHECKING",
131
136
  activeTasksCount: 0,
132
137
  pagesScanned: 0,
@@ -166,53 +171,143 @@ describe("db.websitalk.scans", () => {
166
171
  });
167
172
  });
168
173
 
169
- describe("getActiveScanStatus()", () => {
170
- it("should return isActive: true with the status when the latest scan is still in progress", async () => {
171
- const clientId = "activeClient";
174
+ describe("in-progress scan helpers", () => {
175
+ const websiteUrlId = "website-123";
172
176
 
177
+ it("should return website scan history newest first", async () => {
173
178
  await getScansCollection().insertMany([
174
179
  createScanFixture({
175
- clientId,
180
+ websiteUrlId,
176
181
  status: "COMPLETED",
177
182
  createdAt: new Date("2023-05-01"),
178
183
  }),
179
184
  createScanFixture({
180
- clientId,
181
- status: "SCANNING",
185
+ websiteUrlId,
186
+ status: "COMPLETED",
182
187
  createdAt: new Date("2023-05-15"),
183
188
  }),
184
189
  ]);
185
190
 
186
- const result = await getActiveScanStatus(clientId);
191
+ const scans = await getScansByWebsiteUrl(websiteUrlId);
192
+ expect(scans).toHaveLength(2);
193
+ expect(scans[0].createdAt.getTime()).toBeGreaterThan(
194
+ scans[1].createdAt.getTime(),
195
+ );
196
+ });
197
+
198
+ it("should return the newest scan for a website", async () => {
199
+ await getScansCollection().insertMany([
200
+ createScanFixture({
201
+ websiteUrlId,
202
+ status: "COMPLETED",
203
+ createdAt: new Date("2023-05-01"),
204
+ }),
205
+ createScanFixture({
206
+ websiteUrlId,
207
+ status: "FAILED",
208
+ createdAt: new Date("2023-05-15"),
209
+ }),
210
+ ]);
187
211
 
188
- expect(result).toEqual({ isActive: true, status: "SCANNING" });
212
+ const latest = await getLatestScanByWebsiteUrl(websiteUrlId);
213
+ expect(latest?.status).toBe("FAILED");
189
214
  });
190
215
 
191
- it("should return isActive: false when the latest scan finished (COMPLETED / FAILED)", async () => {
192
- const clientId = "finishedClient";
216
+ it("should return in-progress scan and return null when only completed exists", async () => {
217
+ await getScansCollection().insertMany([
218
+ createScanFixture({
219
+ websiteUrlId,
220
+ status: "COMPLETED",
221
+ createdAt: new Date("2023-05-01"),
222
+ }),
223
+ createScanFixture({
224
+ websiteUrlId,
225
+ status: "SCANNING",
226
+ createdAt: new Date("2023-05-15"),
227
+ }),
228
+ ]);
193
229
 
230
+ const inProgressScan = await getInProgressScanByWebsiteUrl(websiteUrlId);
231
+ expect(inProgressScan?.status).toBe("SCANNING");
232
+
233
+ await getScansCollection().deleteMany({ websiteUrlId });
234
+ await getScansCollection().insertOne(
235
+ createScanFixture({
236
+ websiteUrlId,
237
+ status: "COMPLETED",
238
+ createdAt: new Date("2023-05-20"),
239
+ }),
240
+ );
241
+
242
+ expect(await getInProgressScanByWebsiteUrl(websiteUrlId)).toBeNull();
243
+ });
244
+
245
+ it("should return all in-progress scans across websites for a client", async () => {
246
+ const clientId = "multiScanClient";
194
247
  await getScansCollection().insertMany([
195
248
  createScanFixture({
196
249
  clientId,
250
+ websiteUrlId: "website-a",
197
251
  status: "SCANNING",
198
- createdAt: new Date("2023-05-01"),
199
252
  }),
200
253
  createScanFixture({
201
254
  clientId,
202
- status: "FAILED",
203
- createdAt: new Date("2023-05-15"),
255
+ websiteUrlId: "website-b",
256
+ status: "PROCESSING",
257
+ }),
258
+ createScanFixture({
259
+ clientId,
260
+ websiteUrlId: "website-c",
261
+ status: "COMPLETED",
204
262
  }),
205
263
  ]);
206
264
 
207
- const result = await getActiveScanStatus(clientId);
265
+ const scans = await findInProgressScansByClient(clientId);
266
+ expect(scans).toHaveLength(2);
267
+ expect(scans.map((s) => s.websiteUrlId).sort()).toEqual([
268
+ "website-a",
269
+ "website-b",
270
+ ]);
271
+ });
272
+ });
208
273
 
209
- expect(result).toEqual({ isActive: false });
274
+ describe("website delete helpers", () => {
275
+ it("should return the newest completed scan only", async () => {
276
+ const websiteUrlId = new ObjectId().toString();
277
+ await getScansCollection().insertMany([
278
+ createScanFixture({
279
+ websiteUrlId,
280
+ status: "FAILED",
281
+ createdAt: new Date("2023-05-25"),
282
+ }),
283
+ createScanFixture({
284
+ websiteUrlId,
285
+ status: "COMPLETED",
286
+ createdAt: new Date("2023-05-20"),
287
+ }),
288
+ createScanFixture({
289
+ websiteUrlId,
290
+ status: "COMPLETED",
291
+ createdAt: new Date("2023-05-22"),
292
+ }),
293
+ ]);
294
+
295
+ const latest = await getLatestCompletedScanByWebsiteUrl(websiteUrlId);
296
+ expect(latest?.status).toBe("COMPLETED");
297
+ expect(latest?.createdAt).toEqual(new Date("2023-05-22"));
210
298
  });
211
299
 
212
- it("should return isActive: false when the client has no scans", async () => {
213
- const result = await getActiveScanStatus("noScansClient");
300
+ it("should delete all scans for the website and return count", async () => {
301
+ const websiteUrlId = new ObjectId().toString();
302
+ await getScansCollection().insertMany([
303
+ createScanFixture({ websiteUrlId }),
304
+ createScanFixture({ websiteUrlId }),
305
+ createScanFixture({ websiteUrlId: new ObjectId().toString() }),
306
+ ]);
214
307
 
215
- expect(result).toEqual({ isActive: false });
308
+ const deleted = await deleteScansByWebsiteUrl(websiteUrlId);
309
+ expect(deleted).toBe(2);
310
+ expect(await countScans({ websiteUrlId })).toBe(0);
216
311
  });
217
312
  });
218
313
  });
@@ -1,2 +1,4 @@
1
+ export * from "./scans.constants";
1
2
  export * from "./scans.getters";
2
3
  export * from "./scans.types";
4
+ export * from "./scans.utils";
@@ -0,0 +1,15 @@
1
+ export const SCAN_STATUSES = {
2
+ CHECKING: "CHECKING",
3
+ PREPARING: "PREPARING",
4
+ SCANNING: "SCANNING",
5
+ PROCESSING: "PROCESSING",
6
+ COMPLETED: "COMPLETED",
7
+ FAILED: "FAILED",
8
+ OVERWRITTEN: "OVERWRITTEN",
9
+ } as const;
10
+
11
+ export const FINISHED_SCAN_STATUSES = [
12
+ SCAN_STATUSES.COMPLETED,
13
+ SCAN_STATUSES.FAILED,
14
+ SCAN_STATUSES.OVERWRITTEN,
15
+ ] as const;
@@ -4,12 +4,9 @@ import {
4
4
  applyQueryOptions,
5
5
  QueryOptions,
6
6
  } from "../../talkpilot/utils/query.utils";
7
- import type {
8
- ActiveScanStatus,
9
- Scan,
10
- ScanDoc,
11
- ScanStatus,
12
- } from "./scans.types";
7
+ import type { Scan, ScanDoc } from "./scans.types";
8
+ import { FINISHED_SCAN_STATUSES, SCAN_STATUSES } from "./scans.constants";
9
+ import { isInProgressScan } from "./scans.utils";
13
10
 
14
11
  export const getScansCollection = (): Collection<Scan> => {
15
12
  return getDb().collection<Scan>("scans");
@@ -57,11 +54,12 @@ export const getScanFieldsByClientAndDateRange = <K extends keyof Scan>(
57
54
  };
58
55
 
59
56
  export const createScanDoc = (
60
- scan: Pick<Scan, "clientId" | "baseUrl" | "notifyPhoneNumber">,
57
+ scan: Pick<Scan, "clientId" | "websiteUrlId"> &
58
+ Partial<Pick<Scan, "notifyPhoneNumber">>,
61
59
  ) => {
62
60
  return getScansCollection().insertOne({
63
61
  ...scan,
64
- status: "CHECKING",
62
+ status: SCAN_STATUSES.CHECKING,
65
63
  activeTasksCount: 0,
66
64
  pagesScanned: 0,
67
65
  pagesSkipped: 0,
@@ -72,21 +70,48 @@ export const createScanDoc = (
72
70
  });
73
71
  };
74
72
 
75
- const FINISHED_SCAN_STATUSES: ScanStatus[] = ["COMPLETED", "FAILED"];
73
+ export const getScanById = async (id: string): Promise<ScanDoc | null> => {
74
+ return getScansCollection().findOne({ _id: new ObjectId(id) });
75
+ };
76
76
 
77
- export const getActiveScanStatus = async (
78
- clientId: string,
79
- ): Promise<ActiveScanStatus> => {
80
- const latestScan = await getScansCollection().findOne(
81
- { clientId },
77
+ export const getScansByWebsiteUrl = async (
78
+ websiteUrlId: string,
79
+ ): Promise<ScanDoc[]> => {
80
+ return getScansCollection()
81
+ .find({ websiteUrlId })
82
+ .sort({ createdAt: -1 })
83
+ .toArray();
84
+ };
85
+
86
+ export const getLatestScanByWebsiteUrl = async (
87
+ websiteUrlId: string,
88
+ ): Promise<ScanDoc | null> => {
89
+ return getScansCollection().findOne(
90
+ { websiteUrlId },
82
91
  { sort: { createdAt: -1 } },
83
92
  );
93
+ };
84
94
 
85
- if (!latestScan || FINISHED_SCAN_STATUSES.includes(latestScan.status)) {
86
- return { isActive: false };
95
+ export const getInProgressScanByWebsiteUrl = async (
96
+ websiteUrlId: string,
97
+ ): Promise<ScanDoc | null> => {
98
+ const latestScan = await getLatestScanByWebsiteUrl(websiteUrlId);
99
+ if (!latestScan || !isInProgressScan(latestScan)) {
100
+ return null;
87
101
  }
102
+ return latestScan;
103
+ };
88
104
 
89
- return { isActive: true, status: latestScan.status };
105
+ export const findInProgressScansByClient = async (
106
+ clientId: string,
107
+ ): Promise<ScanDoc[]> => {
108
+ return getScansCollection()
109
+ .find({
110
+ clientId,
111
+ status: { $nin: FINISHED_SCAN_STATUSES },
112
+ })
113
+ .sort({ createdAt: -1 })
114
+ .toArray();
90
115
  };
91
116
 
92
117
  export const updateScanDoc = async (
@@ -111,3 +136,25 @@ export const findScansByQuery = async (
111
136
  export const countScans = async (query: Filter<Scan>): Promise<number> => {
112
137
  return getScansCollection().countDocuments(query);
113
138
  };
139
+
140
+ export const hasScansForWebsiteUrl = async (
141
+ websiteUrlId: string,
142
+ ): Promise<boolean> => {
143
+ return (await countScans({ websiteUrlId })) > 0;
144
+ };
145
+
146
+ export const getLatestCompletedScanByWebsiteUrl = async (
147
+ websiteUrlId: string,
148
+ ): Promise<ScanDoc | null> => {
149
+ return getScansCollection().findOne(
150
+ { websiteUrlId, status: SCAN_STATUSES.COMPLETED },
151
+ { sort: { createdAt: -1 } },
152
+ );
153
+ };
154
+
155
+ export const deleteScansByWebsiteUrl = async (
156
+ websiteUrlId: string,
157
+ ): Promise<number> => {
158
+ const result = await getScansCollection().deleteMany({ websiteUrlId });
159
+ return result.deletedCount;
160
+ };
@@ -1,16 +1,8 @@
1
1
  import { WithId } from "mongodb";
2
2
 
3
- export type ScanStatus =
4
- | "CHECKING"
5
- | "PREPARING"
6
- | "SCANNING"
7
- | "PROCESSING"
8
- | "COMPLETED"
9
- | "FAILED";
3
+ import type { SCAN_STATUSES } from "./scans.constants";
10
4
 
11
- export type ActiveScanStatus =
12
- | { isActive: true; status: ScanStatus }
13
- | { isActive: false };
5
+ export type ScanStatus = (typeof SCAN_STATUSES)[keyof typeof SCAN_STATUSES];
14
6
 
15
7
  export type SiteChromeEntry = {
16
8
  url: string;
@@ -38,7 +30,7 @@ export type SkippedEntry = {
38
30
 
39
31
  export type Scan = {
40
32
  clientId: string;
41
- baseUrl: string;
33
+ websiteUrlId: string;
42
34
  notifyPhoneNumber?: string | null;
43
35
  status: ScanStatus;
44
36
  activeTasksCount: number;
@@ -0,0 +1,5 @@
1
+ import type { Scan, ScanStatus } from "./scans.types";
2
+ import { FINISHED_SCAN_STATUSES } from "./scans.constants";
3
+
4
+ export const isInProgressScan = (scan: Scan): boolean =>
5
+ !(FINISHED_SCAN_STATUSES as readonly ScanStatus[]).includes(scan.status);
@@ -0,0 +1,77 @@
1
+ import {
2
+ createWebsiteUrl,
3
+ getWebsiteUrlsByClient,
4
+ getWebsiteUrlById,
5
+ getWebsiteUrlsCollection,
6
+ updateWebsiteUrl,
7
+ deleteWebsiteUrl,
8
+ } from "../websiteUrls.getters";
9
+ import { createWebsiteUrlFixture } from "../../../test-utils/factories/websitalk/websiteUrls";
10
+
11
+ describe("db.websitalk.websiteUrls", () => {
12
+ const clientId = "test-client";
13
+
14
+ describe("createWebsiteUrl", () => {
15
+ it("should create a website URL configuration", async () => {
16
+ const id = await createWebsiteUrl({
17
+ clientId,
18
+ name: "Primary",
19
+ baseUrl: "https://example.com",
20
+ });
21
+
22
+ const saved = await getWebsiteUrlById(id.toString());
23
+ expect(saved).toMatchObject({
24
+ clientId,
25
+ name: "Primary",
26
+ baseUrl: "https://example.com",
27
+ });
28
+ expect(saved?.createdAt).toBeInstanceOf(Date);
29
+ expect(saved?.updatedAt).toBeInstanceOf(Date);
30
+ });
31
+ });
32
+
33
+ describe("getWebsiteUrlsByClient", () => {
34
+ it("should return all configurations for a client", async () => {
35
+ await getWebsiteUrlsCollection().insertMany([
36
+ createWebsiteUrlFixture({ clientId, name: "A" }),
37
+ createWebsiteUrlFixture({ clientId, name: "B" }),
38
+ createWebsiteUrlFixture({ clientId: "other-client", name: "Other" }),
39
+ ]);
40
+
41
+ const results = await getWebsiteUrlsByClient(clientId);
42
+ expect(results).toHaveLength(2);
43
+ expect(results.map((doc) => doc.name)).toEqual(["A", "B"]);
44
+ });
45
+ });
46
+
47
+ describe("updateWebsiteUrl", () => {
48
+ it("should update name and baseUrl", async () => {
49
+ const id = await createWebsiteUrl({
50
+ clientId,
51
+ name: "Old",
52
+ baseUrl: "https://old.example",
53
+ });
54
+
55
+ const updated = await updateWebsiteUrl(id.toString(), {
56
+ name: "New",
57
+ baseUrl: "https://new.example",
58
+ });
59
+
60
+ expect(updated?.name).toBe("New");
61
+ expect(updated?.baseUrl).toBe("https://new.example");
62
+ });
63
+ });
64
+
65
+ describe("deleteWebsiteUrl", () => {
66
+ it("should delete the website and return true", async () => {
67
+ const id = await createWebsiteUrl({
68
+ clientId: "delete-client",
69
+ name: "Gone",
70
+ baseUrl: "https://gone.example",
71
+ });
72
+
73
+ expect(await deleteWebsiteUrl(id.toString())).toBe(true);
74
+ expect(await getWebsiteUrlById(id.toString())).toBeNull();
75
+ });
76
+ });
77
+ });
@@ -0,0 +1,2 @@
1
+ export * from "./websiteUrls.types";
2
+ export * from "./websiteUrls.getters";
@@ -0,0 +1,65 @@
1
+ import { Collection, Filter, ObjectId as MongoObjectId } from "mongodb";
2
+ import { getDb, ObjectId } from "../index";
3
+ import type {
4
+ CreateWebsiteUrlInput,
5
+ WebsiteUrl,
6
+ WebsiteUrlDoc,
7
+ } from "./websiteUrls.types";
8
+
9
+ export const getWebsiteUrlsCollection = (): Collection<WebsiteUrl> => {
10
+ return getDb().collection<WebsiteUrl>("websiteUrls");
11
+ };
12
+
13
+ export const getWebsiteUrlsByClient = async (
14
+ clientId: string,
15
+ ): Promise<WebsiteUrlDoc[]> => {
16
+ return getWebsiteUrlsCollection()
17
+ .find({ clientId })
18
+ .sort({ createdAt: 1 })
19
+ .toArray();
20
+ };
21
+
22
+ export const getWebsiteUrlById = async (
23
+ id: string,
24
+ ): Promise<WebsiteUrlDoc | null> => {
25
+ return getWebsiteUrlsCollection().findOne({ _id: new ObjectId(id) });
26
+ };
27
+
28
+ export const findWebsiteUrls = async (
29
+ filter: Filter<WebsiteUrl> = {},
30
+ ): Promise<WebsiteUrlDoc[]> => {
31
+ return getWebsiteUrlsCollection().find(filter).toArray();
32
+ };
33
+
34
+ export const createWebsiteUrl = async (
35
+ input: CreateWebsiteUrlInput,
36
+ ): Promise<MongoObjectId> => {
37
+ const now = new Date();
38
+ const { insertedId } = await getWebsiteUrlsCollection().insertOne({
39
+ clientId: input.clientId,
40
+ name: input.name,
41
+ baseUrl: input.baseUrl,
42
+ createdAt: now,
43
+ updatedAt: now,
44
+ });
45
+
46
+ return insertedId;
47
+ };
48
+
49
+ export const updateWebsiteUrl = async (
50
+ id: string,
51
+ updates: Partial<Pick<WebsiteUrl, "name" | "baseUrl">>,
52
+ ): Promise<WebsiteUrlDoc | null> => {
53
+ return getWebsiteUrlsCollection().findOneAndUpdate(
54
+ { _id: new ObjectId(id) },
55
+ { $set: { ...updates, updatedAt: new Date() } },
56
+ { returnDocument: "after" },
57
+ );
58
+ };
59
+
60
+ export const deleteWebsiteUrl = async (id: string): Promise<boolean> => {
61
+ const result = await getWebsiteUrlsCollection().deleteOne({
62
+ _id: new ObjectId(id),
63
+ });
64
+ return result.deletedCount === 1;
65
+ };
@@ -0,0 +1,13 @@
1
+ import { ObjectId } from "mongodb";
2
+
3
+ export type WebsiteUrl = {
4
+ clientId: string;
5
+ name: string;
6
+ baseUrl: string;
7
+ createdAt: Date;
8
+ updatedAt: Date;
9
+ };
10
+
11
+ export type WebsiteUrlDoc = WebsiteUrl & { _id: ObjectId };
12
+
13
+ export type CreateWebsiteUrlInput = Omit<WebsiteUrl, "createdAt" | "updatedAt">;
package/tsconfig.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "target": "ES2022",
4
- "module": "CommonJS",
5
- "moduleResolution": "node",
4
+ "module": "Node16",
5
+ "moduleResolution": "node16",
6
+ "isolatedModules": true,
6
7
  "lib": ["ES2022"],
7
8
  "declaration": true,
8
9
  "declarationMap": true,
@@ -15,8 +16,7 @@
15
16
  "skipLibCheck": true,
16
17
  "resolveJsonModule": true,
17
18
  "experimentalDecorators": true,
18
- "emitDecoratorMetadata": true,
19
- "baseUrl": "."
19
+ "emitDecoratorMetadata": true
20
20
  },
21
21
  "include": ["src/**/*"],
22
22
  "exclude": ["node_modules", "dist", "test", "**/*.test.ts", "**/__tests__/*", "**/*.spec.ts"]