@talkpilot/core-db 1.3.13 → 1.3.15

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 (108) hide show
  1. package/README.md +57 -50
  2. package/README_OLD.md +160 -0
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +1 -0
  6. package/dist/index.js.map +1 -1
  7. package/dist/municipal/index.d.ts +1 -1
  8. package/dist/municipal/index.d.ts.map +1 -1
  9. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +3 -0
  10. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
  11. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +17 -1
  12. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
  13. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +1 -2
  14. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -1
  15. package/dist/talkpilot/flows/flows.schema.d.ts +0 -3
  16. package/dist/talkpilot/flows/flows.schema.d.ts.map +1 -1
  17. package/dist/talkpilot/flows/flows.schema.js +0 -1
  18. package/dist/talkpilot/flows/flows.schema.js.map +1 -1
  19. package/dist/talkpilot/flows/flows.types.d.ts +0 -2
  20. package/dist/talkpilot/flows/flows.types.d.ts.map +1 -1
  21. package/dist/test-utils/factories/index.d.ts +1 -0
  22. package/dist/test-utils/factories/index.d.ts.map +1 -1
  23. package/dist/test-utils/factories/index.js +1 -0
  24. package/dist/test-utils/factories/index.js.map +1 -1
  25. package/dist/test-utils/factories/talkpilot/flows.d.ts.map +1 -1
  26. package/dist/test-utils/factories/talkpilot/flows.js +0 -1
  27. package/dist/test-utils/factories/talkpilot/flows.js.map +1 -1
  28. package/dist/test-utils/factories/websitalk/scans.d.ts.map +1 -1
  29. package/dist/test-utils/factories/websitalk/scans.js +2 -1
  30. package/dist/test-utils/factories/websitalk/scans.js.map +1 -1
  31. package/dist/test-utils/factories/websitalk/websiteUrls.d.ts +5 -0
  32. package/dist/test-utils/factories/websitalk/websiteUrls.d.ts.map +1 -0
  33. package/dist/test-utils/factories/websitalk/websiteUrls.js +17 -0
  34. package/dist/test-utils/factories/websitalk/websiteUrls.js.map +1 -0
  35. package/dist/websitalk/index.d.ts +1 -0
  36. package/dist/websitalk/index.d.ts.map +1 -1
  37. package/dist/websitalk/index.js +1 -0
  38. package/dist/websitalk/index.js.map +1 -1
  39. package/dist/websitalk/scans/index.d.ts +1 -0
  40. package/dist/websitalk/scans/index.d.ts.map +1 -1
  41. package/dist/websitalk/scans/index.js +1 -0
  42. package/dist/websitalk/scans/index.js.map +1 -1
  43. package/dist/websitalk/scans/scans.constants.d.ts +5 -0
  44. package/dist/websitalk/scans/scans.constants.d.ts.map +1 -0
  45. package/dist/websitalk/scans/scans.constants.js +12 -0
  46. package/dist/websitalk/scans/scans.constants.js.map +1 -0
  47. package/dist/websitalk/scans/scans.getters.d.ts +10 -3
  48. package/dist/websitalk/scans/scans.getters.d.ts.map +1 -1
  49. package/dist/websitalk/scans/scans.getters.js +48 -9
  50. package/dist/websitalk/scans/scans.getters.js.map +1 -1
  51. package/dist/websitalk/scans/scans.types.d.ts +2 -8
  52. package/dist/websitalk/scans/scans.types.d.ts.map +1 -1
  53. package/dist/websitalk/websitalk.setup.d.ts +3 -0
  54. package/dist/websitalk/websitalk.setup.d.ts.map +1 -0
  55. package/dist/websitalk/websitalk.setup.js +34 -0
  56. package/dist/websitalk/websitalk.setup.js.map +1 -0
  57. package/dist/websitalk/websiteUrls/index.d.ts +3 -0
  58. package/dist/websitalk/websiteUrls/index.d.ts.map +1 -0
  59. package/dist/websitalk/websiteUrls/index.js +19 -0
  60. package/dist/websitalk/websiteUrls/index.js.map +1 -0
  61. package/dist/websitalk/websiteUrls/websiteUrls.constants.d.ts +2 -0
  62. package/dist/websitalk/websiteUrls/websiteUrls.constants.d.ts.map +1 -0
  63. package/dist/websitalk/websiteUrls/websiteUrls.constants.js +5 -0
  64. package/dist/websitalk/websiteUrls/websiteUrls.constants.js.map +1 -0
  65. package/dist/websitalk/websiteUrls/websiteUrls.context.d.ts +11 -0
  66. package/dist/websitalk/websiteUrls/websiteUrls.context.d.ts.map +1 -0
  67. package/dist/websitalk/websiteUrls/websiteUrls.context.js +35 -0
  68. package/dist/websitalk/websiteUrls/websiteUrls.context.js.map +1 -0
  69. package/dist/websitalk/websiteUrls/websiteUrls.getters.d.ts +10 -0
  70. package/dist/websitalk/websiteUrls/websiteUrls.getters.d.ts.map +1 -0
  71. package/dist/websitalk/websiteUrls/websiteUrls.getters.js +47 -0
  72. package/dist/websitalk/websiteUrls/websiteUrls.getters.js.map +1 -0
  73. package/dist/websitalk/websiteUrls/websiteUrls.setters.d.ts +7 -0
  74. package/dist/websitalk/websiteUrls/websiteUrls.setters.d.ts.map +1 -0
  75. package/dist/websitalk/websiteUrls/websiteUrls.setters.js +29 -0
  76. package/dist/websitalk/websiteUrls/websiteUrls.setters.js.map +1 -0
  77. package/dist/websitalk/websiteUrls/websiteUrls.types.d.ts +13 -0
  78. package/dist/websitalk/websiteUrls/websiteUrls.types.d.ts.map +1 -0
  79. package/dist/websitalk/websiteUrls/websiteUrls.types.js +3 -0
  80. package/dist/websitalk/websiteUrls/websiteUrls.types.js.map +1 -0
  81. package/package.json +1 -1
  82. package/src/index.ts +1 -0
  83. package/src/municipal/index.ts +1 -1
  84. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.getters.spec.ts +57 -5
  85. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +0 -4
  86. package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +24 -0
  87. package/src/talkpilot/clientsConfig/clientsConfig.types.ts +1 -2
  88. package/src/talkpilot/flows/flows.schema.ts +0 -1
  89. package/src/talkpilot/flows/flows.types.ts +0 -2
  90. package/src/test-utils/factories/index.ts +1 -0
  91. package/src/test-utils/factories/talkpilot/flows.ts +0 -1
  92. package/src/test-utils/factories/websitalk/scans.ts +2 -1
  93. package/src/test-utils/factories/websitalk/websiteUrls.ts +17 -0
  94. package/src/websitalk/index.ts +1 -0
  95. package/src/websitalk/scans/__tests__/scans.spec.ts +121 -26
  96. package/src/websitalk/scans/index.ts +1 -0
  97. package/src/websitalk/scans/scans.constants.ts +12 -0
  98. package/src/websitalk/scans/scans.getters.ts +67 -17
  99. package/src/websitalk/scans/scans.types.ts +3 -6
  100. package/src/websitalk/websiteUrls/__tests__/websiteUrls.spec.ts +77 -0
  101. package/src/websitalk/websiteUrls/index.ts +2 -0
  102. package/src/websitalk/websiteUrls/websiteUrls.getters.ts +65 -0
  103. package/src/websitalk/websiteUrls/websiteUrls.types.ts +13 -0
  104. package/tsconfig.json +4 -4
  105. package/dist/talkpilot/subscriptions/subscriptions.utils.d.ts +0 -4
  106. package/dist/talkpilot/subscriptions/subscriptions.utils.d.ts.map +0 -1
  107. package/dist/talkpilot/subscriptions/subscriptions.utils.js +0 -20
  108. package/dist/talkpilot/subscriptions/subscriptions.utils.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websiteUrls.setters.js","sourceRoot":"","sources":["../../../src/websitalk/websiteUrls/websiteUrls.setters.ts"],"names":[],"mappings":";;;AACA,oCAAoC;AACpC,+DAAoF;AAQ7E,MAAM,gBAAgB,GAAG,KAAK,EACnC,KAA4B,EACJ,EAAE;IAC1B,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;IACvB,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,IAAA,8CAAwB,GAAE,CAAC,SAAS,CAAC;QAChE,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,SAAS,EAAE,GAAG;QACd,SAAS,EAAE,GAAG;KACf,CAAC,CAAC;IAEH,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC;AAbW,QAAA,gBAAgB,oBAa3B;AAEK,MAAM,gBAAgB,GAAG,KAAK,EACnC,EAAU,EACV,OAAsD,EACvB,EAAE;IACjC,OAAO,IAAA,8CAAwB,GAAE,CAAC,gBAAgB,CAChD,EAAE,GAAG,EAAE,IAAI,gBAAQ,CAAC,EAAE,CAAC,EAAE,EACzB,EAAE,IAAI,EAAE,EAAE,GAAG,OAAO,EAAE,SAAS,EAAE,IAAI,IAAI,EAAE,EAAE,EAAE,EAC/C,EAAE,cAAc,EAAE,OAAO,EAAE,CAC5B,CAAC;AACJ,CAAC,CAAC;AATW,QAAA,gBAAgB,oBAS3B;AAEK,MAAM,gBAAgB,GAAG,KAAK,EAAE,EAAU,EAAoB,EAAE;IACrE,MAAM,MAAM,GAAG,MAAM,IAAA,8CAAwB,GAAE,CAAC,SAAS,CAAC;QACxD,GAAG,EAAE,IAAI,gBAAQ,CAAC,EAAE,CAAC;KACtB,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC;AACjC,CAAC,CAAC;AANW,QAAA,gBAAgB,oBAM3B"}
@@ -0,0 +1,13 @@
1
+ import { ObjectId } from "mongodb";
2
+ export type WebsiteUrl = {
3
+ clientId: string;
4
+ name: string;
5
+ baseUrl: string;
6
+ createdAt: Date;
7
+ updatedAt: Date;
8
+ };
9
+ export type WebsiteUrlDoc = WebsiteUrl & {
10
+ _id: ObjectId;
11
+ };
12
+ export type CreateWebsiteUrlInput = Omit<WebsiteUrl, "createdAt" | "updatedAt">;
13
+ //# sourceMappingURL=websiteUrls.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websiteUrls.types.d.ts","sourceRoot":"","sources":["../../../src/websitalk/websiteUrls/websiteUrls.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,UAAU,GAAG;IAAE,GAAG,EAAE,QAAQ,CAAA;CAAE,CAAC;AAE3D,MAAM,MAAM,qBAAqB,GAAG,IAAI,CAAC,UAAU,EAAE,WAAW,GAAG,WAAW,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=websiteUrls.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websiteUrls.types.js","sourceRoot":"","sources":["../../../src/websitalk/websiteUrls/websiteUrls.types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talkpilot/core-db",
3
- "version": "1.3.13",
3
+ "version": "1.3.15",
4
4
  "description": "Core database package for centralized connections and ORM integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -24,3 +24,4 @@ export {
24
24
  } from "./websitalk";
25
25
  export * from "./websitalk/scans/scans.getters";
26
26
  export type * from "./websitalk/scans/scans.types";
27
+ export * from "./websitalk/websiteUrls";
@@ -7,7 +7,7 @@ export * from "./tickets";
7
7
  export * from "./systemInstructions";
8
8
  export * from "./muniIssues";
9
9
  export * from "./utils/types";
10
- export { CityName } from "./utils/types";
10
+ export type { CityName } from "./utils/types";
11
11
  export { municipalDataMongodbClient } from "./mongodb-client";
12
12
 
13
13
  let db: Db;
@@ -1,6 +1,9 @@
1
1
  import { describe, it, expect, beforeEach, afterEach } from "@jest/globals";
2
2
  import {
3
3
  getClientsConfigCollection,
4
+ getActiveScanId,
5
+ updateActiveScanId,
6
+ clearActiveScanId,
4
7
  updateProductConfig,
5
8
  } from "../clientsConfig.getters";
6
9
 
@@ -23,8 +26,6 @@ describe("updateProductConfig", () => {
23
26
  websiteTalk: {
24
27
  language: "English",
25
28
  companyName: "OldCorp",
26
- defaultBaseUrl: "https://old-url.com",
27
- logoUrl: "",
28
29
  timezone: "UTC",
29
30
  },
30
31
  },
@@ -38,9 +39,7 @@ describe("updateProductConfig", () => {
38
39
  expect(updatedDoc).toBeDefined();
39
40
  expect(updatedDoc?.products.websiteTalk?.language).toBe("Hebrew");
40
41
  expect(updatedDoc?.products.websiteTalk?.companyName).toBe("TestCorp");
41
- expect(updatedDoc?.products.websiteTalk?.defaultBaseUrl).toBe(
42
- "https://old-url.com",
43
- );
42
+ expect(updatedDoc?.products.websiteTalk?.timezone).toBe("UTC");
44
43
  });
45
44
 
46
45
  it("should throw an error if no client config is found", async () => {
@@ -51,3 +50,56 @@ describe("updateProductConfig", () => {
51
50
  ).rejects.toThrow("No client config found for clientId: invalid-client");
52
51
  });
53
52
  });
53
+
54
+ describe("activeScanId", () => {
55
+ beforeEach(async () => {
56
+ await getClientsConfigCollection().deleteMany({});
57
+ });
58
+
59
+ afterEach(async () => {
60
+ await getClientsConfigCollection().deleteMany({});
61
+ });
62
+
63
+ it("should return undefined when activeScanId is not set", async () => {
64
+ const clientId = "client-active";
65
+ await getClientsConfigCollection().insertOne({
66
+ clientId,
67
+ products: {
68
+ websiteTalk: { companyName: "X", timezone: "UTC", language: "he" },
69
+ },
70
+ } as any);
71
+
72
+ expect(await getActiveScanId(clientId)).toBeUndefined();
73
+ });
74
+
75
+ it("should persist activeScanId and return it from getActiveScanId", async () => {
76
+ const clientId = "client-active-2";
77
+ await getClientsConfigCollection().insertOne({
78
+ clientId,
79
+ products: {
80
+ websiteTalk: { companyName: "X", timezone: "UTC", language: "he" },
81
+ },
82
+ } as any);
83
+
84
+ await updateActiveScanId(clientId, "scan-abc");
85
+ expect(await getActiveScanId(clientId)).toBe("scan-abc");
86
+ });
87
+
88
+ it("should clear activeScanId and return undefined from getActiveScanId", async () => {
89
+ const clientId = "client-active-3";
90
+ await getClientsConfigCollection().insertOne({
91
+ clientId,
92
+ products: {
93
+ websiteTalk: {
94
+ companyName: "X",
95
+ timezone: "UTC",
96
+ language: "he",
97
+ activeScanId: "scan-old",
98
+ },
99
+ },
100
+ } as any);
101
+
102
+ await clearActiveScanId(clientId);
103
+ expect(await getActiveScanId(clientId)).toBeUndefined();
104
+ });
105
+ });
@@ -31,11 +31,9 @@ describe("db.clientsConfig", () => {
31
31
  clientId,
32
32
  products: {
33
33
  websiteTalk: {
34
- logoUrl: "https://cdn.example.com/branding/acme/logo.png",
35
34
  companyName: "Acme Ltd",
36
35
  timezone: "Asia/Jerusalem",
37
36
  language: "he",
38
- defaultBaseUrl: "https://acme.example",
39
37
  },
40
38
  },
41
39
  });
@@ -48,11 +46,9 @@ describe("db.clientsConfig", () => {
48
46
  clientId,
49
47
  products: {
50
48
  websiteTalk: {
51
- logoUrl: "https://cdn.example.com/branding/acme/logo.png",
52
49
  companyName: "Acme Ltd",
53
50
  timezone: "Asia/Jerusalem",
54
51
  language: "he",
55
- defaultBaseUrl: "https://acme.example",
56
52
  },
57
53
  },
58
54
  });
@@ -53,3 +53,27 @@ export async function updateProductConfig<K extends KnownProductKey>(
53
53
  throw new Error(`No client config found for clientId: ${clientId}`);
54
54
  }
55
55
  }
56
+
57
+ export const getActiveScanId = async (
58
+ clientId: string,
59
+ ): Promise<string | undefined> => {
60
+ const cfg = await getClientConfig(clientId);
61
+ return cfg?.products?.websiteTalk?.activeScanId;
62
+ };
63
+
64
+ export const updateActiveScanId = async (
65
+ clientId: string,
66
+ scanId: string,
67
+ ): Promise<void> => {
68
+ await updateProductConfig(clientId, "websiteTalk", { activeScanId: scanId });
69
+ };
70
+
71
+ export const clearActiveScanId = async (clientId: string): Promise<void> => {
72
+ const result = await getClientsConfigCollection().updateOne(
73
+ { clientId },
74
+ { $unset: { "products.websiteTalk.activeScanId": "" } },
75
+ );
76
+ if (result.matchedCount === 0) {
77
+ throw new Error(`No client config found for clientId: ${clientId}`);
78
+ }
79
+ };
@@ -58,11 +58,10 @@ export type WebsiteTalkScheduler = {
58
58
 
59
59
  /** Product config for the websiteTalk */
60
60
  export type WebsiteTalkProduct = {
61
- logoUrl: string;
62
61
  companyName: string;
63
62
  timezone: string;
64
63
  language: string;
65
- defaultBaseUrl: string;
64
+ activeScanId?: string;
66
65
  };
67
66
 
68
67
  // ---------------------------------------------------------------------------
@@ -43,7 +43,6 @@ export const flowMongoSchema = {
43
43
  },
44
44
  additionalProperties: false,
45
45
  },
46
- useRedisTranscription: { bsonType: "bool" },
47
46
  interactions: {
48
47
  bsonType: "array",
49
48
  items: {
@@ -167,8 +167,6 @@ export type Flow = {
167
167
 
168
168
  insights: string[];
169
169
  conversationSettings?: ConversationSettings;
170
- /** When true, prefer cached transcription from Redis (default: false). */
171
- useRedisTranscription?: boolean;
172
170
 
173
171
  interactions: Interaction[];
174
172
 
@@ -13,3 +13,4 @@ export * from "./municipal/muniIssues";
13
13
  export * from "./municipal/streets";
14
14
  export * from "./municipal/tickets";
15
15
  export * from "./websitalk/scans";
16
+ export * from "./websitalk/websiteUrls";
@@ -13,7 +13,6 @@ export const flowFactory = Factory.define<Flow & { _id: ObjectId }>(() => {
13
13
  initialSentence: faker.lorem.sentence(),
14
14
  insights: [],
15
15
  interactions: [],
16
- useRedisTranscription: false,
17
16
  createdAt: now,
18
17
  updatedAt: now,
19
18
  };
@@ -1,10 +1,11 @@
1
1
  import { Factory } from "fishery";
2
2
  import { faker } from "@faker-js/faker";
3
+ import { ObjectId } from "mongodb";
3
4
  import type { Scan } from "../../../websitalk";
4
5
 
5
6
  export const scanFactory = Factory.define<Scan>(() => ({
6
7
  clientId: faker.string.uuid(),
7
- baseUrl: faker.internet.url(),
8
+ websiteUrlId: new ObjectId().toString(),
8
9
  status: "CHECKING",
9
10
  activeTasksCount: 0,
10
11
  pagesScanned: 0,
@@ -0,0 +1,17 @@
1
+ import { Factory } from "fishery";
2
+ import { faker } from "@faker-js/faker";
3
+ import type { WebsiteUrl } from "../../../websitalk/websiteUrls";
4
+
5
+ export const websiteUrlFactory = Factory.define<WebsiteUrl>(() => ({
6
+ clientId: faker.string.uuid(),
7
+ name: faker.company.name(),
8
+ baseUrl: faker.internet.url(),
9
+ createdAt: new Date(),
10
+ updatedAt: new Date(),
11
+ }));
12
+
13
+ export function createWebsiteUrlFixture(
14
+ overrides?: Partial<WebsiteUrl>,
15
+ ): WebsiteUrl {
16
+ return websiteUrlFactory.build(overrides);
17
+ }
@@ -1,6 +1,7 @@
1
1
  import { Db, ObjectId as MongoObjectId } from "mongodb";
2
2
 
3
3
  export * from "./scans";
4
+ export * from "./websiteUrls";
4
5
  export { websitalkMongodbClient } from "./mongodb-client";
5
6
 
6
7
  let db: Db;
@@ -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,3 @@
1
+ export * from "./scans.constants";
1
2
  export * from "./scans.getters";
2
3
  export * from "./scans.types";
@@ -0,0 +1,12 @@
1
+ import type { Scan, ScanStatus } from "./scans.types";
2
+
3
+ export const INITIAL_SCAN_STATUS = "CHECKING" as const satisfies ScanStatus;
4
+
5
+ export const FINISHED_SCAN_STATUSES: ScanStatus[] = [
6
+ "COMPLETED",
7
+ "FAILED",
8
+ "OVERWRITTEN",
9
+ ];
10
+
11
+ export const isInProgressScan = (scan: Scan): boolean =>
12
+ !FINISHED_SCAN_STATUSES.includes(scan.status);
@@ -4,12 +4,12 @@ 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 {
9
+ FINISHED_SCAN_STATUSES,
10
+ INITIAL_SCAN_STATUS,
11
+ isInProgressScan,
12
+ } from "./scans.constants";
13
13
 
14
14
  export const getScansCollection = (): Collection<Scan> => {
15
15
  return getDb().collection<Scan>("scans");
@@ -57,11 +57,12 @@ export const getScanFieldsByClientAndDateRange = <K extends keyof Scan>(
57
57
  };
58
58
 
59
59
  export const createScanDoc = (
60
- scan: Pick<Scan, "clientId" | "baseUrl" | "notifyPhoneNumber">,
60
+ scan: Pick<Scan, "clientId" | "websiteUrlId"> &
61
+ Partial<Pick<Scan, "notifyPhoneNumber">>,
61
62
  ) => {
62
63
  return getScansCollection().insertOne({
63
64
  ...scan,
64
- status: "CHECKING",
65
+ status: INITIAL_SCAN_STATUS,
65
66
  activeTasksCount: 0,
66
67
  pagesScanned: 0,
67
68
  pagesSkipped: 0,
@@ -72,21 +73,48 @@ export const createScanDoc = (
72
73
  });
73
74
  };
74
75
 
75
- const FINISHED_SCAN_STATUSES: ScanStatus[] = ["COMPLETED", "FAILED"];
76
+ export const getScanById = async (id: string): Promise<ScanDoc | null> => {
77
+ return getScansCollection().findOne({ _id: new ObjectId(id) });
78
+ };
76
79
 
77
- export const getActiveScanStatus = async (
78
- clientId: string,
79
- ): Promise<ActiveScanStatus> => {
80
- const latestScan = await getScansCollection().findOne(
81
- { clientId },
80
+ export const getScansByWebsiteUrl = async (
81
+ websiteUrlId: string,
82
+ ): Promise<ScanDoc[]> => {
83
+ return getScansCollection()
84
+ .find({ websiteUrlId })
85
+ .sort({ createdAt: -1 })
86
+ .toArray();
87
+ };
88
+
89
+ export const getLatestScanByWebsiteUrl = async (
90
+ websiteUrlId: string,
91
+ ): Promise<ScanDoc | null> => {
92
+ return getScansCollection().findOne(
93
+ { websiteUrlId },
82
94
  { sort: { createdAt: -1 } },
83
95
  );
96
+ };
84
97
 
85
- if (!latestScan || FINISHED_SCAN_STATUSES.includes(latestScan.status)) {
86
- return { isActive: false };
98
+ export const getInProgressScanByWebsiteUrl = async (
99
+ websiteUrlId: string,
100
+ ): Promise<ScanDoc | null> => {
101
+ const latestScan = await getLatestScanByWebsiteUrl(websiteUrlId);
102
+ if (!latestScan || !isInProgressScan(latestScan)) {
103
+ return null;
87
104
  }
105
+ return latestScan;
106
+ };
88
107
 
89
- return { isActive: true, status: latestScan.status };
108
+ export const findInProgressScansByClient = async (
109
+ clientId: string,
110
+ ): Promise<ScanDoc[]> => {
111
+ return getScansCollection()
112
+ .find({
113
+ clientId,
114
+ status: { $nin: FINISHED_SCAN_STATUSES },
115
+ })
116
+ .sort({ createdAt: -1 })
117
+ .toArray();
90
118
  };
91
119
 
92
120
  export const updateScanDoc = async (
@@ -111,3 +139,25 @@ export const findScansByQuery = async (
111
139
  export const countScans = async (query: Filter<Scan>): Promise<number> => {
112
140
  return getScansCollection().countDocuments(query);
113
141
  };
142
+
143
+ export const hasScansForWebsiteUrl = async (
144
+ websiteUrlId: string,
145
+ ): Promise<boolean> => {
146
+ return (await countScans({ websiteUrlId })) > 0;
147
+ };
148
+
149
+ export const getLatestCompletedScanByWebsiteUrl = async (
150
+ websiteUrlId: string,
151
+ ): Promise<ScanDoc | null> => {
152
+ return getScansCollection().findOne(
153
+ { websiteUrlId, status: "COMPLETED" },
154
+ { sort: { createdAt: -1 } },
155
+ );
156
+ };
157
+
158
+ export const deleteScansByWebsiteUrl = async (
159
+ websiteUrlId: string,
160
+ ): Promise<number> => {
161
+ const result = await getScansCollection().deleteMany({ websiteUrlId });
162
+ return result.deletedCount;
163
+ };