@opentermsarchive/engine 5.0.2 → 5.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentermsarchive/engine",
3
- "version": "5.0.2",
3
+ "version": "5.0.3",
4
4
  "description": "Tracks and makes visible changes to the terms of online services",
5
5
  "homepage": "https://opentermsarchive.org",
6
6
  "bugs": {
@@ -34,14 +34,14 @@ export default class MongoRepository extends RepositoryInterface {
34
34
  }
35
35
 
36
36
  async save(record) {
37
- const { serviceId, termsType } = record;
37
+ const { serviceId, termsType, documentId } = record;
38
38
 
39
39
  if (record.isFirstRecord === undefined || record.isFirstRecord === null) {
40
- record.isFirstRecord = !await this.collection.findOne({ serviceId, termsType });
40
+ record.isFirstRecord = !await this.collection.findOne({ serviceId, termsType, documentId });
41
41
  }
42
42
 
43
43
  const documentFields = await this.#toPersistence(record);
44
- const previousRecord = await this.findLatest(serviceId, termsType);
44
+ const previousRecord = await this.findLatest(serviceId, termsType, documentId);
45
45
 
46
46
  if (previousRecord?.content == documentFields.content) {
47
47
  return Object(null);
@@ -54,14 +54,26 @@ export default class MongoRepository extends RepositoryInterface {
54
54
  return record;
55
55
  }
56
56
 
57
- async findLatest(serviceId, termsType) {
58
- const [mongoDocument] = await this.collection.find({ serviceId, termsType }).limit(1).sort({ fetchDate: -1 }).toArray(); // `findOne` doesn't support the `sort` method, so even for only one mongo document use `find`
57
+ async findLatest(serviceId, termsType, documentId) {
58
+ const query = { serviceId, termsType };
59
+
60
+ if (documentId !== undefined) {
61
+ query.documentId = documentId;
62
+ }
63
+
64
+ const [mongoDocument] = await this.collection.find(query).limit(1).sort({ fetchDate: -1 }).toArray(); // `findOne` doesn't support the `sort` method, so even for only one mongo document use `find`
59
65
 
60
66
  return this.#toDomain(mongoDocument);
61
67
  }
62
68
 
63
- async findByDate(serviceId, termsType, date) {
64
- const [mongoDocument] = await this.collection.find({ serviceId, termsType, fetchDate: { $lte: new Date(date) } }).limit(1).sort({ fetchDate: -1 }).toArray(); // `findOne` doesn't support the `sort` method, so even for only one mongo document use `find`
69
+ async findByDate(serviceId, termsType, date, documentId) {
70
+ const query = { serviceId, termsType, fetchDate: { $lte: new Date(date) } };
71
+
72
+ if (documentId !== undefined) {
73
+ query.documentId = documentId;
74
+ }
75
+
76
+ const [mongoDocument] = await this.collection.find(query).limit(1).sort({ fetchDate: -1 }).toArray(); // `findOne` doesn't support the `sort` method, so even for only one mongo document use `find`
65
77
 
66
78
  return this.#toDomain(mongoDocument);
67
79
  }
@@ -332,6 +332,30 @@ describe('MongoRepository', () => {
332
332
  expect(mongoDocument.termsType).to.include(TERMS_TYPE);
333
333
  });
334
334
  });
335
+
336
+ context('when document ID is specified', () => {
337
+ before(async () => {
338
+ (record = await subject.save(new Version({
339
+ serviceId: SERVICE_PROVIDER_ID,
340
+ termsType: TERMS_TYPE,
341
+ documentId: DOCUMENT_ID,
342
+ content: CONTENT,
343
+ fetchDate: FETCH_DATE,
344
+ snapshotIds: [SNAPSHOT_ID],
345
+ })));
346
+
347
+ (mongoDocument = await collection.findOne({
348
+ serviceId: SERVICE_PROVIDER_ID,
349
+ termsType: TERMS_TYPE,
350
+ }));
351
+ });
352
+
353
+ after(() => subject.removeAll());
354
+
355
+ it('stores the document ID', () => {
356
+ expect(mongoDocument.documentId).to.equal(DOCUMENT_ID);
357
+ });
358
+ });
335
359
  });
336
360
 
337
361
  describe('#findById', () => {
@@ -439,6 +463,46 @@ describe('MongoRepository', () => {
439
463
  expect(recordFound.id).to.include(recordToFindId);
440
464
  });
441
465
  });
466
+
467
+ context('when document ID is specified', () => {
468
+ let recordFound;
469
+ const DIFFERENT_DOCUMENT_ID = 'other-document';
470
+ const UPDATED_CONTENT = `${CONTENT} (with additional content)`;
471
+
472
+ before(async () => {
473
+ await subject.save(new Version({
474
+ serviceId: SERVICE_PROVIDER_ID,
475
+ termsType: TERMS_TYPE,
476
+ documentId: DOCUMENT_ID,
477
+ content: CONTENT,
478
+ fetchDate: FETCH_DATE,
479
+ snapshotIds: [SNAPSHOT_ID],
480
+ }));
481
+
482
+ await subject.save(new Version({
483
+ serviceId: SERVICE_PROVIDER_ID,
484
+ termsType: TERMS_TYPE,
485
+ documentId: DIFFERENT_DOCUMENT_ID,
486
+ content: UPDATED_CONTENT,
487
+ fetchDate: FETCH_DATE_LATER,
488
+ snapshotIds: [SNAPSHOT_ID],
489
+ }));
490
+
491
+ recordFound = await subject.findByDate(
492
+ SERVICE_PROVIDER_ID,
493
+ TERMS_TYPE,
494
+ FETCH_DATE_LATER,
495
+ DOCUMENT_ID,
496
+ );
497
+ });
498
+
499
+ after(() => subject.removeAll());
500
+
501
+ it('returns only records matching the document ID', () => {
502
+ expect(recordFound.documentId).to.equal(DOCUMENT_ID);
503
+ expect(recordFound.content).to.equal(CONTENT);
504
+ });
505
+ });
442
506
  });
443
507
  });
444
508
 
@@ -580,6 +644,44 @@ describe('MongoRepository', () => {
580
644
  expect((await latestRecord.content).toString('utf8')).to.equal(UPDATED_CONTENT);
581
645
  });
582
646
  });
647
+
648
+ context('when document ID is specified', () => {
649
+ let latestRecord;
650
+ const DIFFERENT_DOCUMENT_ID = 'other-document';
651
+
652
+ before(async () => {
653
+ await subject.save(new Version({
654
+ serviceId: SERVICE_PROVIDER_ID,
655
+ termsType: TERMS_TYPE,
656
+ documentId: DOCUMENT_ID,
657
+ content: CONTENT,
658
+ fetchDate: FETCH_DATE_LATER,
659
+ snapshotIds: [SNAPSHOT_ID],
660
+ }));
661
+
662
+ await subject.save(new Version({
663
+ serviceId: SERVICE_PROVIDER_ID,
664
+ termsType: TERMS_TYPE,
665
+ documentId: DIFFERENT_DOCUMENT_ID,
666
+ content: CONTENT,
667
+ fetchDate: FETCH_DATE,
668
+ snapshotIds: [SNAPSHOT_ID],
669
+ }));
670
+
671
+ latestRecord = await subject.findLatest(
672
+ SERVICE_PROVIDER_ID,
673
+ TERMS_TYPE,
674
+ DIFFERENT_DOCUMENT_ID,
675
+ );
676
+ });
677
+
678
+ after(() => subject.removeAll());
679
+
680
+ it('returns only records matching the document ID', () => {
681
+ expect(latestRecord.documentId).to.equal(DIFFERENT_DOCUMENT_ID);
682
+ expect(latestRecord.fetchDate).to.deep.equal(FETCH_DATE);
683
+ });
684
+ });
583
685
  });
584
686
 
585
687
  context('when there are no records for the given service', () => {
@@ -1119,6 +1221,44 @@ describe('MongoRepository', () => {
1119
1221
  expect(latestRecord.mimeType).to.equal(PDF_MIME_TYPE);
1120
1222
  });
1121
1223
  });
1224
+
1225
+ context('when document ID is specified', () => {
1226
+ let latestRecord;
1227
+ const DIFFERENT_DOCUMENT_ID = 'other-document';
1228
+
1229
+ before(async () => {
1230
+ await subject.save(new Snapshot({
1231
+ serviceId: SERVICE_PROVIDER_ID,
1232
+ termsType: TERMS_TYPE,
1233
+ documentId: DOCUMENT_ID,
1234
+ content: CONTENT,
1235
+ fetchDate: FETCH_DATE_LATER,
1236
+ mimeType: HTML_MIME_TYPE,
1237
+ }));
1238
+
1239
+ await subject.save(new Snapshot({
1240
+ serviceId: SERVICE_PROVIDER_ID,
1241
+ termsType: TERMS_TYPE,
1242
+ documentId: DIFFERENT_DOCUMENT_ID,
1243
+ content: CONTENT,
1244
+ fetchDate: FETCH_DATE,
1245
+ mimeType: HTML_MIME_TYPE,
1246
+ }));
1247
+
1248
+ latestRecord = await subject.findLatest(
1249
+ SERVICE_PROVIDER_ID,
1250
+ TERMS_TYPE,
1251
+ DIFFERENT_DOCUMENT_ID,
1252
+ );
1253
+ });
1254
+
1255
+ after(() => subject.removeAll());
1256
+
1257
+ it('returns only records matching the document ID', () => {
1258
+ expect(latestRecord.documentId).to.equal(DIFFERENT_DOCUMENT_ID);
1259
+ expect(latestRecord.fetchDate).to.deep.equal(FETCH_DATE);
1260
+ });
1261
+ });
1122
1262
  });
1123
1263
 
1124
1264
  context('when there are no records for the given service', () => {