فهرست منبع

fix(syncing-server): updating with missing creation date (#824)

Karol Sójko 1 سال پیش
والد
کامیت
3a8607d146

+ 48 - 2
packages/syncing-server/src/Domain/UseCase/Syncing/UpdateExistingItem/UpdateExistingItem.spec.ts

@@ -298,7 +298,7 @@ describe('UpdateExistingItem', () => {
     expect(itemRepository.save).toHaveBeenCalled()
   })
 
-  it('should return error if created at time is not give in any form', async () => {
+  it('should fallback to updated at timestamp if created at time is not give in any form', async () => {
     const useCase = createUseCase()
 
     const result = await useCase.execute({
@@ -308,13 +308,59 @@ describe('UpdateExistingItem', () => {
         ...itemHash1.props,
         created_at: undefined,
         created_at_timestamp: undefined,
+        updated_at_timestamp: 123,
       }).getValue(),
       sessionUuid: '00000000-0000-0000-0000-000000000000',
       performingUserUuid: '00000000-0000-0000-0000-000000000000',
       roleNames: [RoleName.NAMES.CoreUser],
     })
 
-    expect(result.isFailed()).toBeTruthy()
+    expect(result.isFailed()).toBeFalsy()
+    expect(itemRepository.save).toHaveBeenCalled()
+  })
+
+  it('should fallback to updated at date if created at time is not give in any form', async () => {
+    const useCase = createUseCase()
+
+    const result = await useCase.execute({
+      existingItem: item1,
+      onGoingRevisionsTransition: false,
+      itemHash: ItemHash.create({
+        ...itemHash1.props,
+        created_at: undefined,
+        created_at_timestamp: undefined,
+        updated_at_timestamp: undefined,
+        updated_at: '2020-01-01T00:00:00.000Z',
+      }).getValue(),
+      sessionUuid: '00000000-0000-0000-0000-000000000000',
+      performingUserUuid: '00000000-0000-0000-0000-000000000000',
+      roleNames: [RoleName.NAMES.CoreUser],
+    })
+
+    expect(result.isFailed()).toBeFalsy()
+    expect(itemRepository.save).toHaveBeenCalled()
+  })
+
+  it('should fallback to 0 if created at and update at time is not give in any form', async () => {
+    const useCase = createUseCase()
+
+    const result = await useCase.execute({
+      existingItem: item1,
+      onGoingRevisionsTransition: false,
+      itemHash: ItemHash.create({
+        ...itemHash1.props,
+        created_at: undefined,
+        created_at_timestamp: undefined,
+        updated_at_timestamp: undefined,
+        updated_at: undefined,
+      }).getValue(),
+      sessionUuid: '00000000-0000-0000-0000-000000000000',
+      performingUserUuid: '00000000-0000-0000-0000-000000000000',
+      roleNames: [RoleName.NAMES.CoreUser],
+    })
+
+    expect(result.isFailed()).toBeFalsy()
+    expect(itemRepository.save).toHaveBeenCalled()
   })
 
   it('should return error if dates could not be created from timestamps', async () => {

+ 25 - 11
packages/syncing-server/src/Domain/UseCase/Syncing/UpdateExistingItem/UpdateExistingItem.ts

@@ -23,6 +23,7 @@ import { SharedVaultOperationOnItem } from '../../../SharedVault/SharedVaultOper
 import { AddNotificationForUser } from '../../Messaging/AddNotificationForUser/AddNotificationForUser'
 import { RemoveNotificationsForUser } from '../../Messaging/RemoveNotificationsForUser/RemoveNotificationsForUser'
 import { ItemRepositoryResolverInterface } from '../../../Item/ItemRepositoryResolverInterface'
+import { ItemHash } from '../../../Item/ItemHash'
 
 export class UpdateExistingItem implements UseCaseInterface<Item> {
   constructor(
@@ -115,17 +116,7 @@ export class UpdateExistingItem implements UseCaseInterface<Item> {
     )
     const updatedAtDate = this.timer.convertMicrosecondsToDate(updatedAtTimestamp)
 
-    let createdAtTimestamp: number
-    let createdAtDate: Date
-    if (dto.itemHash.props.created_at_timestamp) {
-      createdAtTimestamp = dto.itemHash.props.created_at_timestamp
-      createdAtDate = this.timer.convertMicrosecondsToDate(createdAtTimestamp)
-    } else if (dto.itemHash.props.created_at) {
-      createdAtTimestamp = this.timer.convertStringDateToMicroseconds(dto.itemHash.props.created_at)
-      createdAtDate = this.timer.convertStringDateToDate(dto.itemHash.props.created_at)
-    } else {
-      return Result.fail('Created at timestamp is required.')
-    }
+    const { createdAtDate, createdAtTimestamp } = this.determineCreatedAt(dto.itemHash)
 
     const datesOrError = Dates.create(createdAtDate, updatedAtDate)
     if (datesOrError.isFailed()) {
@@ -221,6 +212,29 @@ export class UpdateExistingItem implements UseCaseInterface<Item> {
     return Result.ok(dto.existingItem)
   }
 
+  private determineCreatedAt(itemHash: ItemHash): { createdAtDate: Date; createdAtTimestamp: number } {
+    let createdAtTimestamp: number
+    let createdAtDate: Date
+    if (itemHash.props.created_at_timestamp) {
+      createdAtTimestamp = itemHash.props.created_at_timestamp
+      createdAtDate = this.timer.convertMicrosecondsToDate(createdAtTimestamp)
+    } else if (itemHash.props.created_at) {
+      createdAtTimestamp = this.timer.convertStringDateToMicroseconds(itemHash.props.created_at)
+      createdAtDate = this.timer.convertStringDateToDate(itemHash.props.created_at)
+    } else if (itemHash.props.updated_at_timestamp) {
+      createdAtTimestamp = itemHash.props.updated_at_timestamp
+      createdAtDate = this.timer.convertMicrosecondsToDate(itemHash.props.updated_at_timestamp)
+    } else if (itemHash.props.updated_at) {
+      createdAtTimestamp = this.timer.convertStringDateToMicroseconds(itemHash.props.updated_at)
+      createdAtDate = this.timer.convertStringDateToDate(itemHash.props.updated_at)
+    } else {
+      createdAtTimestamp = 0
+      createdAtDate = new Date(0)
+    }
+
+    return { createdAtDate, createdAtTimestamp }
+  }
+
   private async addNotificationsAndPublishEvents(
     userUuid: Uuid,
     sharedVaultOperation: SharedVaultOperationOnItem | null,