@talkpilot/core-db 1.3.28 → 1.3.30

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 (104) hide show
  1. package/README.md +23 -12
  2. package/dist/index.d.ts +0 -6
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +1 -6
  5. package/dist/index.js.map +1 -1
  6. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts +1 -15
  7. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts.map +1 -1
  8. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js +1 -24
  9. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js.map +1 -1
  10. package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts +0 -1
  11. package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts.map +1 -1
  12. package/dist/municipal/streets/streets.getters.d.ts +1 -2
  13. package/dist/municipal/streets/streets.getters.d.ts.map +1 -1
  14. package/dist/municipal/streets/streets.getters.js +1 -6
  15. package/dist/municipal/streets/streets.getters.js.map +1 -1
  16. package/dist/municipal/streets/streets.types.d.ts +0 -1
  17. package/dist/municipal/streets/streets.types.d.ts.map +1 -1
  18. package/dist/talkpilot/clients/clients.quota.constants.d.ts +2 -0
  19. package/dist/talkpilot/clients/clients.quota.constants.d.ts.map +1 -0
  20. package/dist/talkpilot/clients/clients.quota.constants.js +5 -0
  21. package/dist/talkpilot/clients/clients.quota.constants.js.map +1 -0
  22. package/dist/talkpilot/clients/clients.quota.getters.d.ts +7 -0
  23. package/dist/talkpilot/clients/clients.quota.getters.d.ts.map +1 -0
  24. package/dist/talkpilot/clients/clients.quota.getters.js +80 -0
  25. package/dist/talkpilot/clients/clients.quota.getters.js.map +1 -0
  26. package/dist/talkpilot/clients/clients.quota.types.d.ts +22 -0
  27. package/dist/talkpilot/clients/clients.quota.types.d.ts.map +1 -0
  28. package/dist/{bulkWrite/bulkWrite.types.js → talkpilot/clients/clients.quota.types.js} +1 -1
  29. package/dist/talkpilot/clients/clients.quota.types.js.map +1 -0
  30. package/dist/talkpilot/clients/clients.quota.utils.d.ts +8 -0
  31. package/dist/talkpilot/clients/clients.quota.utils.d.ts.map +1 -0
  32. package/dist/talkpilot/clients/clients.quota.utils.js +45 -0
  33. package/dist/talkpilot/clients/clients.quota.utils.js.map +1 -0
  34. package/dist/talkpilot/clients/clients.types.d.ts +3 -0
  35. package/dist/talkpilot/clients/clients.types.d.ts.map +1 -1
  36. package/dist/talkpilot/clients/index.d.ts +2 -0
  37. package/dist/talkpilot/clients/index.d.ts.map +1 -1
  38. package/dist/talkpilot/clients/index.js +2 -0
  39. package/dist/talkpilot/clients/index.js.map +1 -1
  40. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +5 -0
  41. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
  42. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +37 -1
  43. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
  44. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +3 -0
  45. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -1
  46. package/package.json +1 -1
  47. package/src/index.ts +0 -7
  48. package/src/municipal/departmentsSubjects/departmentsSubjects.getters.ts +3 -38
  49. package/src/municipal/departmentsSubjects/departmentsSubjects.types.ts +0 -1
  50. package/src/municipal/streets/streets.getters.ts +1 -16
  51. package/src/municipal/streets/streets.types.ts +0 -1
  52. package/src/talkpilot/clients/__tests__/clients.quota.spec.ts +123 -0
  53. package/src/talkpilot/clients/clients.quota.getters.ts +119 -0
  54. package/src/talkpilot/clients/clients.quota.types.ts +25 -0
  55. package/src/talkpilot/clients/clients.types.ts +4 -0
  56. package/src/talkpilot/clients/index.ts +2 -0
  57. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +21 -0
  58. package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +54 -0
  59. package/src/talkpilot/clientsConfig/clientsConfig.types.ts +4 -0
  60. package/dist/bulkWrite/bulkWrite.ops.d.ts +0 -17
  61. package/dist/bulkWrite/bulkWrite.ops.d.ts.map +0 -1
  62. package/dist/bulkWrite/bulkWrite.ops.js +0 -35
  63. package/dist/bulkWrite/bulkWrite.ops.js.map +0 -1
  64. package/dist/bulkWrite/bulkWrite.types.d.ts +0 -4
  65. package/dist/bulkWrite/bulkWrite.types.d.ts.map +0 -1
  66. package/dist/bulkWrite/bulkWrite.types.js.map +0 -1
  67. package/dist/bulkWrite/index.d.ts +0 -3
  68. package/dist/bulkWrite/index.d.ts.map +0 -1
  69. package/dist/bulkWrite/index.js +0 -19
  70. package/dist/bulkWrite/index.js.map +0 -1
  71. package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.d.ts +0 -10
  72. package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.d.ts.map +0 -1
  73. package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.js +0 -15
  74. package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.js.map +0 -1
  75. package/dist/municipal/streets/streets.setters.d.ts +0 -10
  76. package/dist/municipal/streets/streets.setters.d.ts.map +0 -1
  77. package/dist/municipal/streets/streets.setters.js +0 -15
  78. package/dist/municipal/streets/streets.setters.js.map +0 -1
  79. package/dist/reconcile/index.d.ts +0 -5
  80. package/dist/reconcile/index.d.ts.map +0 -1
  81. package/dist/reconcile/index.js +0 -21
  82. package/dist/reconcile/index.js.map +0 -1
  83. package/dist/reconcile/reconcile.diff.d.ts +0 -4
  84. package/dist/reconcile/reconcile.diff.d.ts.map +0 -1
  85. package/dist/reconcile/reconcile.diff.js +0 -36
  86. package/dist/reconcile/reconcile.diff.js.map +0 -1
  87. package/dist/reconcile/reconcile.execute.d.ts +0 -7
  88. package/dist/reconcile/reconcile.execute.d.ts.map +0 -1
  89. package/dist/reconcile/reconcile.execute.js +0 -20
  90. package/dist/reconcile/reconcile.execute.js.map +0 -1
  91. package/dist/reconcile/reconcile.ops.d.ts +0 -12
  92. package/dist/reconcile/reconcile.ops.d.ts.map +0 -1
  93. package/dist/reconcile/reconcile.ops.js +0 -24
  94. package/dist/reconcile/reconcile.ops.js.map +0 -1
  95. package/dist/reconcile/reconcile.types.d.ts +0 -32
  96. package/dist/reconcile/reconcile.types.d.ts.map +0 -1
  97. package/dist/reconcile/reconcile.types.js +0 -3
  98. package/dist/reconcile/reconcile.types.js.map +0 -1
  99. package/src/bulkWrite/__tests__/bulkWrite.spec.ts +0 -65
  100. package/src/bulkWrite/bulkWrite.ops.ts +0 -45
  101. package/src/bulkWrite/bulkWrite.types.ts +0 -4
  102. package/src/bulkWrite/index.ts +0 -2
  103. package/src/municipal/departmentsSubjects/__tests__/departmentsSubjects.spec.ts +0 -272
  104. package/src/municipal/streets/__tests__/streets.spec.ts +0 -253
@@ -1,272 +0,0 @@
1
- import { ObjectId } from 'mongodb';
2
- import {
3
- getDepartmentsSubjectsCollection,
4
- getAllDepartmentsSubjectsByCity,
5
- bulkWriteDepartmentsSubjects,
6
- findDepartmentSubjectsByFilter,
7
- updateDepartmentSubjectActive,
8
- } from '../departmentsSubjects.getters';
9
- import { createDepartmentSubject } from '../../../test-utils/factories/municipal/departmentsSubjects';
10
-
11
- const CITY = 'tests' as const;
12
-
13
- describe('DepartmentSubject — isActive field', () => {
14
- describe('persistence', () => {
15
- it('a document inserted without isActive is retrievable and the field is absent', async () => {
16
- const subject = createDepartmentSubject({ cityName: CITY });
17
- delete (subject as Partial<typeof subject>).isActive;
18
-
19
- await getDepartmentsSubjectsCollection().insertOne(subject);
20
-
21
- const saved = await getDepartmentsSubjectsCollection().findOne({ _id: subject._id });
22
- expect(saved).not.toBeNull();
23
- expect(saved).not.toHaveProperty('isActive');
24
- });
25
-
26
- it('isActive: true is persisted and returned', async () => {
27
- const subject = createDepartmentSubject({ cityName: CITY, isActive: true });
28
- await getDepartmentsSubjectsCollection().insertOne(subject);
29
-
30
- const saved = await getDepartmentsSubjectsCollection().findOne({ _id: subject._id });
31
- expect(saved?.isActive).toBe(true);
32
- });
33
-
34
- it('isActive: false is persisted and returned', async () => {
35
- const subject = createDepartmentSubject({ cityName: CITY, isActive: false });
36
- await getDepartmentsSubjectsCollection().insertOne(subject);
37
-
38
- const saved = await getDepartmentsSubjectsCollection().findOne({ _id: subject._id });
39
- expect(saved?.isActive).toBe(false);
40
- });
41
- });
42
-
43
- describe('querying', () => {
44
- it('getAllDepartmentsSubjectsByCity returns all subjects regardless of isActive', async () => {
45
- const activeSubject = createDepartmentSubject({ cityName: CITY, isActive: true });
46
- const inactiveSubject = createDepartmentSubject({ cityName: CITY, isActive: false });
47
- const noFlagSubject = createDepartmentSubject({ cityName: CITY });
48
- delete (noFlagSubject as Partial<typeof noFlagSubject>).isActive;
49
-
50
- await getDepartmentsSubjectsCollection().insertMany([activeSubject, inactiveSubject, noFlagSubject]);
51
-
52
- const all = await getAllDepartmentsSubjectsByCity(CITY);
53
- expect(all).toHaveLength(3);
54
-
55
- const active = all.find((s) => s._id.equals(activeSubject._id));
56
- const inactive = all.find((s) => s._id.equals(inactiveSubject._id));
57
- const noFlag = all.find((s) => s._id.equals(noFlagSubject._id));
58
-
59
- expect(active?.isActive).toBe(true);
60
- expect(inactive?.isActive).toBe(false);
61
- expect(noFlag?.isActive).toBeUndefined(); // absent in DB → getter is a pass-through, no normalization
62
- });
63
-
64
- it('filtering with isActive !== false excludes inactive and includes active and unset documents', async () => {
65
- const activeSubject = createDepartmentSubject({ cityName: CITY, isActive: true });
66
- const inactiveSubject = createDepartmentSubject({ cityName: CITY, isActive: false });
67
- const noFlagSubject = createDepartmentSubject({ cityName: CITY });
68
- delete (noFlagSubject as Partial<typeof noFlagSubject>).isActive;
69
-
70
- await getDepartmentsSubjectsCollection().insertMany([activeSubject, inactiveSubject, noFlagSubject]);
71
-
72
- // This is the exact filter pattern used by the sync script to skip already-inactive records
73
- const notInactive = await getDepartmentsSubjectsCollection()
74
- .find({ cityName: CITY, isActive: { $ne: false } })
75
- .toArray();
76
-
77
- expect(notInactive).toHaveLength(2);
78
- expect(notInactive.map((s) => s._id.toString())).toEqual(
79
- expect.arrayContaining([activeSubject._id.toString(), noFlagSubject._id.toString()])
80
- );
81
- expect(notInactive.map((s) => s._id.toString())).not.toContain(inactiveSubject._id.toString());
82
- });
83
- });
84
-
85
- describe('updating', () => {
86
- it('isActive can be set to false on an existing active document (deactivation)', async () => {
87
- const subject = createDepartmentSubject({ cityName: CITY, isActive: true });
88
- await getDepartmentsSubjectsCollection().insertOne(subject);
89
-
90
- await getDepartmentsSubjectsCollection().updateOne(
91
- { _id: subject._id },
92
- { $set: { isActive: false, updatedAt: new Date() } }
93
- );
94
-
95
- const updated = await getDepartmentsSubjectsCollection().findOne({ _id: subject._id });
96
- expect(updated?.isActive).toBe(false);
97
- });
98
-
99
- it('isActive can be set to true on a deactivated document (reactivation)', async () => {
100
- const subject = createDepartmentSubject({ cityName: CITY, isActive: false });
101
- await getDepartmentsSubjectsCollection().insertOne(subject);
102
-
103
- await getDepartmentsSubjectsCollection().updateOne(
104
- { _id: subject._id },
105
- { $set: { isActive: true, updatedAt: new Date() } }
106
- );
107
-
108
- const updated = await getDepartmentsSubjectsCollection().findOne({ _id: subject._id });
109
- expect(updated?.isActive).toBe(true);
110
- });
111
-
112
- it('deactivating a subject does not touch other fields', async () => {
113
- const subject = createDepartmentSubject({
114
- cityName: CITY,
115
- isActive: true,
116
- guidelines: 'handle with care',
117
- instructions: [{ id: 1, instruction: 'call back within 2h' }],
118
- sla: 48,
119
- });
120
- await getDepartmentsSubjectsCollection().insertOne(subject);
121
-
122
- await getDepartmentsSubjectsCollection().updateOne(
123
- { _id: subject._id },
124
- { $set: { isActive: false, updatedAt: new Date() } }
125
- );
126
-
127
- const updated = await getDepartmentsSubjectsCollection().findOne({ _id: subject._id });
128
- expect(updated?.isActive).toBe(false);
129
- expect(updated?.guidelines).toBe('handle with care');
130
- expect(updated?.instructions).toEqual([{ id: 1, instruction: 'call back within 2h' }]);
131
- expect(updated?.sla).toBe(48);
132
- });
133
- });
134
-
135
- describe('bulkWriteDepartmentsSubjects', () => {
136
- it('applies upsert and update operations from a single batch', async () => {
137
- const existing = createDepartmentSubject({ cityName: CITY, subjectName: 'Old Name' });
138
- await getDepartmentsSubjectsCollection().insertOne(existing);
139
-
140
- const result = await bulkWriteDepartmentsSubjects(
141
- [
142
- {
143
- updateOne: {
144
- filter: { _id: existing._id },
145
- update: { $set: { subjectName: 'New Name' } },
146
- },
147
- },
148
- {
149
- updateOne: {
150
- filter: { cityName: CITY, subject_id: 'brand-new', sub_subject_id: 'sub-1' },
151
- update: {
152
- $set: { subjectName: 'Brand New Subject', sub_subject_name: 'Sub One' },
153
- $setOnInsert: {
154
- subject_id: 'brand-new',
155
- sub_subject_id: 'sub-1',
156
- cityName: CITY,
157
- instructions: [],
158
- descriptions: [],
159
- createdAt: new Date(),
160
- isActive: true,
161
- },
162
- },
163
- upsert: true,
164
- },
165
- },
166
- ],
167
- { ordered: false }
168
- );
169
-
170
- expect(result.modifiedCount).toBe(1);
171
- expect(result.upsertedCount).toBe(1);
172
-
173
- const updated = await getDepartmentsSubjectsCollection().findOne({ _id: existing._id });
174
- expect(updated?.subjectName).toBe('New Name');
175
-
176
- const inserted = await getDepartmentsSubjectsCollection().findOne({
177
- subject_id: 'brand-new',
178
- sub_subject_id: 'sub-1',
179
- });
180
- expect(inserted?.subjectName).toBe('Brand New Subject');
181
- });
182
-
183
- it('propagates a MongoBulkWriteError for an invalid operation, preserving partial results', async () => {
184
- const existing = createDepartmentSubject({ cityName: CITY });
185
- await getDepartmentsSubjectsCollection().insertOne(existing);
186
-
187
- await expect(
188
- bulkWriteDepartmentsSubjects(
189
- [
190
- {
191
- updateOne: {
192
- filter: { _id: existing._id },
193
- update: { $set: { subjectName: 'Valid Update' } },
194
- },
195
- },
196
- {
197
- insertOne: {
198
- // Duplicate _id triggers a write error without aborting the ordered:false batch
199
- document: { ...existing },
200
- },
201
- },
202
- ],
203
- { ordered: false }
204
- )
205
- ).rejects.toMatchObject({ name: 'MongoBulkWriteError' });
206
-
207
- const updated = await getDepartmentsSubjectsCollection().findOne({ _id: existing._id });
208
- expect(updated?.subjectName).toBe('Valid Update');
209
- });
210
- });
211
-
212
- describe('findDepartmentSubjectsByFilter', () => {
213
- it('returns all documents matching the given filter', async () => {
214
- const match1 = createDepartmentSubject({ cityName: CITY, subject_id: 'r1', sub_subject_id: 's1' });
215
- const match2 = createDepartmentSubject({ cityName: CITY, subject_id: 'r1', sub_subject_id: 's2' });
216
- const nonMatch = createDepartmentSubject({ cityName: CITY, subject_id: 'r2', sub_subject_id: 's3' });
217
- await getDepartmentsSubjectsCollection().insertMany([match1, match2, nonMatch]);
218
-
219
- const docs = await findDepartmentSubjectsByFilter({ cityName: CITY, subject_id: 'r1' });
220
-
221
- expect(docs.map((doc) => doc._id.toString())).toEqual(
222
- expect.arrayContaining([match1._id.toString(), match2._id.toString()])
223
- );
224
- expect(docs).toHaveLength(2);
225
- });
226
-
227
- it('returns an empty array when nothing matches', async () => {
228
- const docs = await findDepartmentSubjectsByFilter({
229
- cityName: CITY,
230
- subject_id: 'does-not-exist',
231
- });
232
- expect(docs).toEqual([]);
233
- });
234
- });
235
-
236
- describe('updateDepartmentSubjectActive', () => {
237
- it('sets isActive on the document with the given _id and returns it', async () => {
238
- const subject = createDepartmentSubject({ cityName: CITY, isActive: true });
239
- await getDepartmentsSubjectsCollection().insertOne(subject);
240
-
241
- const updated = await updateDepartmentSubjectActive(subject._id, false);
242
-
243
- expect(updated?.isActive).toBe(false);
244
- const saved = await getDepartmentsSubjectsCollection().findOne({ _id: subject._id });
245
- expect(saved?.isActive).toBe(false);
246
- });
247
-
248
- it('returns null when no document matches the given _id', async () => {
249
- const updated = await updateDepartmentSubjectActive(new ObjectId(), true);
250
- expect(updated).toBeNull();
251
- });
252
-
253
- it('does not touch other fields when updating isActive', async () => {
254
- const subject = createDepartmentSubject({
255
- cityName: CITY,
256
- isActive: true,
257
- guidelines: 'handle with care',
258
- instructions: [{ id: 1, instruction: 'call back within 2h' }],
259
- sla: 48,
260
- });
261
- await getDepartmentsSubjectsCollection().insertOne(subject);
262
-
263
- await updateDepartmentSubjectActive(subject._id, false);
264
-
265
- const updated = await getDepartmentsSubjectsCollection().findOne({ _id: subject._id });
266
- expect(updated?.isActive).toBe(false);
267
- expect(updated?.guidelines).toBe('handle with care');
268
- expect(updated?.instructions).toEqual([{ id: 1, instruction: 'call back within 2h' }]);
269
- expect(updated?.sla).toBe(48);
270
- });
271
- });
272
- });
@@ -1,253 +0,0 @@
1
- import {
2
- getStreetsCollection,
3
- getStreetsByCityName,
4
- getStreetById,
5
- getStreetByStreetId,
6
- getStreetByStreetIdAndCity,
7
- createStreet,
8
- bulkWriteStreets,
9
- } from '../streets.getters';
10
- import { createStreet as streetFactory } from '../../../test-utils/factories/municipal/streets';
11
-
12
- const CITY = 'tests' as const;
13
-
14
- describe('Street — isActive field', () => {
15
- describe('persistence', () => {
16
- it('a document inserted without isActive is retrievable and the field is absent', async () => {
17
- const street = streetFactory({ cityName: CITY });
18
- delete (street as Partial<typeof street>).isActive;
19
-
20
- await getStreetsCollection().insertOne(street);
21
-
22
- const saved = await getStreetsCollection().findOne({ _id: street._id });
23
- expect(saved).not.toBeNull();
24
- expect(saved).not.toHaveProperty('isActive');
25
- });
26
-
27
- it('isActive: true is persisted and returned', async () => {
28
- const street = streetFactory({ cityName: CITY, isActive: true });
29
- await getStreetsCollection().insertOne(street);
30
-
31
- const saved = await getStreetsCollection().findOne({ _id: street._id });
32
- expect(saved?.isActive).toBe(true);
33
- });
34
-
35
- it('isActive: false is persisted and returned', async () => {
36
- const street = streetFactory({ cityName: CITY, isActive: false });
37
- await getStreetsCollection().insertOne(street);
38
-
39
- const saved = await getStreetsCollection().findOne({ _id: street._id });
40
- expect(saved?.isActive).toBe(false);
41
- });
42
- });
43
-
44
- describe('createStreet', () => {
45
- it('defaults isActive to true when not provided', async () => {
46
- const data = streetFactory({ cityName: CITY });
47
- delete (data as Partial<typeof data>).isActive;
48
-
49
- const insertedId = await createStreet(data);
50
-
51
- const saved = await getStreetsCollection().findOne({ _id: insertedId });
52
- expect(saved?.isActive).toBe(true);
53
- });
54
-
55
- it('persists isActive: false when explicitly passed', async () => {
56
- const data = streetFactory({ cityName: CITY, isActive: false });
57
- const insertedId = await createStreet(data);
58
-
59
- const saved = await getStreetsCollection().findOne({ _id: insertedId });
60
- expect(saved?.isActive).toBe(false);
61
- });
62
- });
63
-
64
- describe('querying', () => {
65
- it('getStreetsByCityName returns all streets regardless of isActive', async () => {
66
- const active = streetFactory({ cityName: CITY, isActive: true });
67
- const inactive = streetFactory({ cityName: CITY, isActive: false });
68
- const noFlag = streetFactory({ cityName: CITY });
69
- delete (noFlag as Partial<typeof noFlag>).isActive;
70
-
71
- await getStreetsCollection().insertMany([active, inactive, noFlag]);
72
-
73
- const all = await getStreetsByCityName(CITY);
74
- expect(all.length).toBeGreaterThanOrEqual(3);
75
-
76
- const returnedActive = all.find((s) => s._id.equals(active._id));
77
- const returnedInactive = all.find((s) => s._id.equals(inactive._id));
78
- const returnedNoFlag = all.find((s) => s._id.equals(noFlag._id));
79
-
80
- expect(returnedActive?.isActive).toBe(true);
81
- expect(returnedInactive?.isActive).toBe(false);
82
- expect(returnedNoFlag?.isActive).toBeUndefined(); // absent in DB → getter is a pass-through, no normalization
83
- });
84
-
85
- it('getStreetById returns isActive undefined when absent in DB', async () => {
86
- const street = streetFactory({ cityName: CITY });
87
- delete (street as Partial<typeof street>).isActive;
88
- await getStreetsCollection().insertOne(street);
89
-
90
- const result = await getStreetById(street._id.toString());
91
- expect(result?.isActive).toBeUndefined();
92
- });
93
-
94
- it('getStreetById returns isActive: false unchanged', async () => {
95
- const street = streetFactory({ cityName: CITY, isActive: false });
96
- await getStreetsCollection().insertOne(street);
97
-
98
- const result = await getStreetById(street._id.toString());
99
- expect(result?.isActive).toBe(false);
100
- });
101
-
102
- it('getStreetByStreetId returns isActive undefined when absent in DB', async () => {
103
- const street = streetFactory({ cityName: CITY });
104
- delete (street as Partial<typeof street>).isActive;
105
- await getStreetsCollection().insertOne(street);
106
-
107
- const result = await getStreetByStreetId(street.id);
108
- expect(result?.isActive).toBeUndefined();
109
- });
110
-
111
- it('getStreetByStreetIdAndCity returns isActive undefined when absent in DB', async () => {
112
- const street = streetFactory({ cityName: CITY });
113
- delete (street as Partial<typeof street>).isActive;
114
- await getStreetsCollection().insertOne(street);
115
-
116
- const result = await getStreetByStreetIdAndCity(street.id, CITY);
117
- expect(result?.isActive).toBeUndefined();
118
- });
119
-
120
- it('filtering with isActive !== false excludes inactive and includes active and unset documents', async () => {
121
- const active = streetFactory({ cityName: CITY, isActive: true });
122
- const inactive = streetFactory({ cityName: CITY, isActive: false });
123
- const noFlag = streetFactory({ cityName: CITY });
124
- delete (noFlag as Partial<typeof noFlag>).isActive;
125
-
126
- await getStreetsCollection().insertMany([active, inactive, noFlag]);
127
-
128
- // This is the exact filter pattern used by the sync script to skip already-inactive records
129
- const notInactive = await getStreetsCollection()
130
- .find({ cityName: CITY, isActive: { $ne: false } })
131
- .toArray();
132
-
133
- expect(notInactive.map((s) => s._id.toString())).toEqual(
134
- expect.arrayContaining([active._id.toString(), noFlag._id.toString()])
135
- );
136
- expect(notInactive.map((s) => s._id.toString())).not.toContain(inactive._id.toString());
137
- });
138
- });
139
-
140
- describe('updating', () => {
141
- it('isActive can be set to false on an existing active document (deactivation)', async () => {
142
- const street = streetFactory({ cityName: CITY, isActive: true });
143
- await getStreetsCollection().insertOne(street);
144
-
145
- await getStreetsCollection().updateOne(
146
- { _id: street._id },
147
- { $set: { isActive: false, updatedAt: new Date() } }
148
- );
149
-
150
- const updated = await getStreetsCollection().findOne({ _id: street._id });
151
- expect(updated?.isActive).toBe(false);
152
- });
153
-
154
- it('isActive can be set to true on a deactivated document (reactivation)', async () => {
155
- const street = streetFactory({ cityName: CITY, isActive: false });
156
- await getStreetsCollection().insertOne(street);
157
-
158
- await getStreetsCollection().updateOne(
159
- { _id: street._id },
160
- { $set: { isActive: true, updatedAt: new Date() } }
161
- );
162
-
163
- const updated = await getStreetsCollection().findOne({ _id: street._id });
164
- expect(updated?.isActive).toBe(true);
165
- });
166
-
167
- it('deactivating a street does not touch other fields', async () => {
168
- const street = streetFactory({
169
- cityName: CITY,
170
- isActive: true,
171
- name: 'Herzl St',
172
- hints: [{ id: '1', text: 'near the park' }],
173
- });
174
- await getStreetsCollection().insertOne(street);
175
-
176
- await getStreetsCollection().updateOne(
177
- { _id: street._id },
178
- { $set: { isActive: false, updatedAt: new Date() } }
179
- );
180
-
181
- const updated = await getStreetsCollection().findOne({ _id: street._id });
182
- expect(updated?.isActive).toBe(false);
183
- expect(updated?.name).toBe('Herzl St');
184
- expect(updated?.hints).toEqual([{ id: '1', text: 'near the park' }]);
185
- });
186
- });
187
-
188
- describe('bulkWriteStreets', () => {
189
- it('applies upsert and update operations from a single batch', async () => {
190
- const existing = streetFactory({ cityName: CITY, name: 'Old Name' });
191
- await getStreetsCollection().insertOne(existing);
192
-
193
- const result = await bulkWriteStreets(
194
- [
195
- {
196
- updateOne: {
197
- filter: { _id: existing._id },
198
- update: { $set: { name: 'New Name' } },
199
- },
200
- },
201
- {
202
- updateOne: {
203
- filter: { id: 'brand-new-street', cityName: CITY },
204
- update: {
205
- $set: { name: 'Brand New St', cityName: CITY },
206
- $setOnInsert: { id: 'brand-new-street', createdAt: new Date(), isActive: true },
207
- },
208
- upsert: true,
209
- },
210
- },
211
- ],
212
- { ordered: false }
213
- );
214
-
215
- expect(result.modifiedCount).toBe(1);
216
- expect(result.upsertedCount).toBe(1);
217
-
218
- const updated = await getStreetsCollection().findOne({ _id: existing._id });
219
- expect(updated?.name).toBe('New Name');
220
-
221
- const inserted = await getStreetsCollection().findOne({ id: 'brand-new-street', cityName: CITY });
222
- expect(inserted?.name).toBe('Brand New St');
223
- });
224
-
225
- it('propagates a MongoBulkWriteError for an invalid operation, preserving partial results', async () => {
226
- const existing = streetFactory({ cityName: CITY });
227
- await getStreetsCollection().insertOne(existing);
228
-
229
- await expect(
230
- bulkWriteStreets(
231
- [
232
- {
233
- updateOne: {
234
- filter: { _id: existing._id },
235
- update: { $set: { name: 'Valid Update' } },
236
- },
237
- },
238
- {
239
- insertOne: {
240
- // Duplicate _id triggers a write error without aborting the ordered:false batch
241
- document: { ...existing },
242
- },
243
- },
244
- ],
245
- { ordered: false }
246
- )
247
- ).rejects.toMatchObject({ name: 'MongoBulkWriteError' });
248
-
249
- const updated = await getStreetsCollection().findOne({ _id: existing._id });
250
- expect(updated?.name).toBe('Valid Update');
251
- });
252
- });
253
- });