mirror of
https://github.com/standardnotes/server
synced 2026-10-11 03:13:42 -04:00
feat(syncing-server): refactor syncing to decouple getting and saving items (#659)
* feat(syncing-server): refactor syncing to decouple getting and saving items * fix(syncing-server): item hash http representation mapping * fix(syncing-server): remove redundant specs for inversify express controller
This commit is contained in:
+1
@@ -32,6 +32,7 @@ export class CreateSharedVault implements UseCaseInterface<CreateSharedVaultResu
|
||||
fileUploadBytesUsed: 0,
|
||||
userUuid,
|
||||
timestamps,
|
||||
sharedVaultItems: [],
|
||||
})
|
||||
if (sharedVaultOrError.isFailed()) {
|
||||
return Result.fail(sharedVaultOrError.getError())
|
||||
|
||||
+1
@@ -24,6 +24,7 @@ describe('CreateSharedVaultFileValetToken', () => {
|
||||
fileUploadBytesUsed: 2,
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
|
||||
sharedVaultRepository = {} as jest.Mocked<SharedVaultRepositoryInterface>
|
||||
|
||||
+2
@@ -31,6 +31,7 @@ describe('DeleteSharedVault', () => {
|
||||
fileUploadBytesUsed: 2,
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
sharedVaultRepository = {} as jest.Mocked<SharedVaultRepositoryInterface>
|
||||
sharedVaultRepository.findByUuid = jest.fn().mockResolvedValue(sharedVault)
|
||||
@@ -115,6 +116,7 @@ describe('DeleteSharedVault', () => {
|
||||
fileUploadBytesUsed: 2,
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000001').getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
sharedVaultRepository.findByUuid = jest.fn().mockResolvedValue(sharedVault)
|
||||
const useCase = createUseCase()
|
||||
|
||||
+2
@@ -20,6 +20,7 @@ describe('GetSharedVaultUsers', () => {
|
||||
fileUploadBytesUsed: 2,
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
|
||||
sharedVaultUser = SharedVaultUser.create({
|
||||
@@ -66,6 +67,7 @@ describe('GetSharedVaultUsers', () => {
|
||||
fileUploadBytesUsed: 2,
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000001').getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
sharedVaultRepository.findByUuid = jest.fn().mockResolvedValue(sharedVault)
|
||||
|
||||
|
||||
+1
@@ -29,6 +29,7 @@ describe('GetSharedVaults', () => {
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
fileUploadBytesLimit: 123,
|
||||
fileUploadBytesUsed: 123,
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
sharedVaultRepository = {} as jest.Mocked<SharedVaultRepositoryInterface>
|
||||
sharedVaultRepository.findByUuids = jest.fn().mockResolvedValue([sharedVault])
|
||||
|
||||
+2
@@ -21,6 +21,7 @@ describe('InviteUserToSharedVault', () => {
|
||||
fileUploadBytesUsed: 2,
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
sharedVaultRepository = {} as jest.Mocked<SharedVaultRepositoryInterface>
|
||||
sharedVaultRepository.findByUuid = jest.fn().mockResolvedValue(sharedVault)
|
||||
@@ -152,6 +153,7 @@ describe('InviteUserToSharedVault', () => {
|
||||
fileUploadBytesUsed: 2,
|
||||
userUuid: Uuid.create('10000000-0000-0000-0000-000000000000').getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
sharedVaultRepository.findByUuid = jest.fn().mockResolvedValue(sharedVault)
|
||||
|
||||
|
||||
+2
@@ -24,6 +24,7 @@ describe('RemoveUserFromSharedVault', () => {
|
||||
fileUploadBytesUsed: 2,
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
sharedVaultRepository = {} as jest.Mocked<SharedVaultRepositoryInterface>
|
||||
sharedVaultRepository.findByUuid = jest.fn().mockResolvedValue(sharedVault)
|
||||
@@ -88,6 +89,7 @@ describe('RemoveUserFromSharedVault', () => {
|
||||
fileUploadBytesUsed: 2,
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000002').getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
sharedVaultItems: [],
|
||||
}).getValue()
|
||||
sharedVaultRepository.findByUuid = jest.fn().mockResolvedValue(sharedVault)
|
||||
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
import { TimerInterface } from '@standardnotes/time'
|
||||
import { ItemRepositoryInterface } from '../../../Item/ItemRepositoryInterface'
|
||||
import { ItemTransferCalculatorInterface } from '../../../Item/ItemTransferCalculatorInterface'
|
||||
import { GetItems } from './GetItems'
|
||||
import { Item } from '../../../Item/Item'
|
||||
import { ContentType, Dates, Timestamps, Uuid } from '@standardnotes/domain-core'
|
||||
|
||||
describe('GetItems', () => {
|
||||
let itemRepository: ItemRepositoryInterface
|
||||
const contentSizeTransferLimit = 100
|
||||
let itemTransferCalculator: ItemTransferCalculatorInterface
|
||||
let timer: TimerInterface
|
||||
const maxItemsSyncLimit = 100
|
||||
let item: Item
|
||||
|
||||
const createUseCase = () =>
|
||||
new GetItems(itemRepository, contentSizeTransferLimit, itemTransferCalculator, timer, maxItemsSyncLimit)
|
||||
|
||||
beforeEach(() => {
|
||||
item = Item.create({
|
||||
duplicateOf: null,
|
||||
itemsKeyId: 'items-key-id',
|
||||
content: 'content',
|
||||
contentType: ContentType.create(ContentType.TYPES.Note).getValue(),
|
||||
encItemKey: 'enc-item-key',
|
||||
authHash: 'auth-hash',
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
|
||||
deleted: false,
|
||||
updatedWithSession: null,
|
||||
dates: Dates.create(new Date(123), new Date(123)).getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
}).getValue()
|
||||
|
||||
itemRepository = {} as jest.Mocked<ItemRepositoryInterface>
|
||||
itemRepository.findAll = jest.fn().mockResolvedValue([item])
|
||||
itemRepository.countAll = jest.fn().mockResolvedValue(1)
|
||||
|
||||
itemTransferCalculator = {} as jest.Mocked<ItemTransferCalculatorInterface>
|
||||
itemTransferCalculator.computeItemUuidsToFetch = jest.fn().mockResolvedValue(['item-uuid'])
|
||||
|
||||
timer = {} as jest.Mocked<TimerInterface>
|
||||
timer.getTimestampInMicroseconds = jest.fn().mockReturnValue(123)
|
||||
timer.convertStringDateToMicroseconds = jest.fn().mockReturnValue(123)
|
||||
})
|
||||
|
||||
it('returns items', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: 'user-uuid',
|
||||
cursorToken: undefined,
|
||||
contentType: undefined,
|
||||
limit: 10,
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue()).toEqual({
|
||||
items: [item],
|
||||
cursorToken: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
it('should return cursor token if there are more items to fetch', async () => {
|
||||
itemRepository.countAll = jest.fn().mockResolvedValue(101)
|
||||
|
||||
const useCase = createUseCase()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: 'user-uuid',
|
||||
cursorToken: undefined,
|
||||
contentType: undefined,
|
||||
limit: undefined,
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue()).toEqual({
|
||||
items: [item],
|
||||
cursorToken: 'MjowLjAwMDEyMw==',
|
||||
})
|
||||
})
|
||||
|
||||
it('should return items based on the cursort token passed', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: 'user-uuid',
|
||||
cursorToken: 'MjowLjAwMDEyMw==',
|
||||
contentType: undefined,
|
||||
limit: undefined,
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue()).toEqual({
|
||||
items: [item],
|
||||
cursorToken: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
it('should return items based on a sync token containing string date', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
const syncTokenData = '1:2021-01-01T00:00:00.000Z'
|
||||
const syncToken = Buffer.from(syncTokenData, 'utf-8').toString('base64')
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: 'user-uuid',
|
||||
syncToken,
|
||||
contentType: undefined,
|
||||
limit: undefined,
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue()).toEqual({
|
||||
items: [item],
|
||||
cursorToken: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
it('should return error if the sync token is invalid', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
const syncTokenData = 'invalid'
|
||||
const syncToken = Buffer.from(syncTokenData, 'utf-8').toString('base64')
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: 'user-uuid',
|
||||
syncToken,
|
||||
contentType: undefined,
|
||||
limit: undefined,
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeTruthy()
|
||||
expect(result.getError()).toEqual('Sync token is missing version part')
|
||||
})
|
||||
|
||||
it('should guard the upper bound limit of items to fetch', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: 'user-uuid',
|
||||
cursorToken: undefined,
|
||||
contentType: undefined,
|
||||
limit: 200,
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue()).toEqual({
|
||||
items: [item],
|
||||
cursorToken: undefined,
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,95 @@
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
import { Time, TimerInterface } from '@standardnotes/time'
|
||||
|
||||
import { Item } from '../../../Item/Item'
|
||||
import { GetItemsResult } from './GetItemsResult'
|
||||
import { ItemQuery } from '../../../Item/ItemQuery'
|
||||
import { ItemRepositoryInterface } from '../../../Item/ItemRepositoryInterface'
|
||||
import { ItemTransferCalculatorInterface } from '../../../Item/ItemTransferCalculatorInterface'
|
||||
import { GetItemsDTO } from './GetItemsDTO'
|
||||
|
||||
export class GetItems implements UseCaseInterface<GetItemsResult> {
|
||||
private readonly DEFAULT_ITEMS_LIMIT = 150
|
||||
private readonly SYNC_TOKEN_VERSION = 2
|
||||
|
||||
constructor(
|
||||
private itemRepository: ItemRepositoryInterface,
|
||||
private contentSizeTransferLimit: number,
|
||||
private itemTransferCalculator: ItemTransferCalculatorInterface,
|
||||
private timer: TimerInterface,
|
||||
private maxItemsSyncLimit: number,
|
||||
) {}
|
||||
|
||||
async execute(dto: GetItemsDTO): Promise<Result<GetItemsResult>> {
|
||||
const lastSyncTimeOrError = this.getLastSyncTime(dto)
|
||||
if (lastSyncTimeOrError.isFailed()) {
|
||||
return Result.fail(lastSyncTimeOrError.getError())
|
||||
}
|
||||
const lastSyncTime = lastSyncTimeOrError.getValue()
|
||||
|
||||
const syncTimeComparison = dto.cursorToken ? '>=' : '>'
|
||||
const limit = dto.limit === undefined || dto.limit < 1 ? this.DEFAULT_ITEMS_LIMIT : dto.limit
|
||||
const upperBoundLimit = limit < this.maxItemsSyncLimit ? limit : this.maxItemsSyncLimit
|
||||
|
||||
const itemQuery: ItemQuery = {
|
||||
userUuid: dto.userUuid,
|
||||
lastSyncTime: lastSyncTime ?? undefined,
|
||||
syncTimeComparison,
|
||||
contentType: dto.contentType,
|
||||
deleted: lastSyncTime ? undefined : false,
|
||||
sortBy: 'updated_at_timestamp',
|
||||
sortOrder: 'ASC',
|
||||
limit: upperBoundLimit,
|
||||
}
|
||||
|
||||
const itemUuidsToFetch = await this.itemTransferCalculator.computeItemUuidsToFetch(
|
||||
itemQuery,
|
||||
this.contentSizeTransferLimit,
|
||||
)
|
||||
let items: Array<Item> = []
|
||||
if (itemUuidsToFetch.length > 0) {
|
||||
items = await this.itemRepository.findAll({
|
||||
uuids: itemUuidsToFetch,
|
||||
sortBy: 'updated_at_timestamp',
|
||||
sortOrder: 'ASC',
|
||||
})
|
||||
}
|
||||
const totalItemsCount = await this.itemRepository.countAll(itemQuery)
|
||||
|
||||
let cursorToken = undefined
|
||||
if (totalItemsCount > upperBoundLimit) {
|
||||
const lastSyncTime = items[items.length - 1].props.timestamps.updatedAt / Time.MicrosecondsInASecond
|
||||
cursorToken = Buffer.from(`${this.SYNC_TOKEN_VERSION}:${lastSyncTime}`, 'utf-8').toString('base64')
|
||||
}
|
||||
|
||||
return Result.ok({
|
||||
items,
|
||||
cursorToken,
|
||||
})
|
||||
}
|
||||
|
||||
private getLastSyncTime(dto: GetItemsDTO): Result<number | null> {
|
||||
let token = dto.syncToken
|
||||
if (dto.cursorToken !== undefined && dto.cursorToken !== null) {
|
||||
token = dto.cursorToken
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
return Result.ok(null)
|
||||
}
|
||||
|
||||
const decodedToken = Buffer.from(token, 'base64').toString('utf-8')
|
||||
|
||||
const tokenParts = decodedToken.split(':')
|
||||
const version = tokenParts.shift()
|
||||
|
||||
switch (version) {
|
||||
case '1':
|
||||
return Result.ok(this.timer.convertStringDateToMicroseconds(tokenParts.join(':')))
|
||||
case '2':
|
||||
return Result.ok(+tokenParts[0] * Time.MicrosecondsInASecond)
|
||||
default:
|
||||
return Result.fail('Sync token is missing version part')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export interface GetItemsDTO {
|
||||
userUuid: string
|
||||
syncToken?: string | null
|
||||
cursorToken?: string | null
|
||||
limit?: number
|
||||
contentType?: string
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Item } from '../../../Item/Item'
|
||||
|
||||
export interface GetItemsResult {
|
||||
items: Item[]
|
||||
cursorToken?: string
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
import { TimerInterface } from '@standardnotes/time'
|
||||
import { ItemRepositoryInterface } from '../../../Item/ItemRepositoryInterface'
|
||||
import { ItemSaveValidatorInterface } from '../../../Item/SaveValidator/ItemSaveValidatorInterface'
|
||||
import { SaveItems } from './SaveItems'
|
||||
import { SaveNewItem } from '../SaveNewItem/SaveNewItem'
|
||||
import { UpdateExistingItem } from '../UpdateExistingItem/UpdateExistingItem'
|
||||
import { Logger } from 'winston'
|
||||
import { ContentType, Dates, Result, Timestamps, Uuid } from '@standardnotes/domain-core'
|
||||
import { ItemHash } from '../../../Item/ItemHash'
|
||||
import { Item } from '../../../Item/Item'
|
||||
|
||||
describe('SaveItems', () => {
|
||||
let itemSaveValidator: ItemSaveValidatorInterface
|
||||
let itemRepository: ItemRepositoryInterface
|
||||
let timer: TimerInterface
|
||||
let saveNewItem: SaveNewItem
|
||||
let updateExistingItem: UpdateExistingItem
|
||||
let logger: Logger
|
||||
let itemHash1: ItemHash
|
||||
let savedItem: Item
|
||||
|
||||
const createUseCase = () =>
|
||||
new SaveItems(itemSaveValidator, itemRepository, timer, saveNewItem, updateExistingItem, logger)
|
||||
|
||||
beforeEach(() => {
|
||||
itemSaveValidator = {} as jest.Mocked<ItemSaveValidatorInterface>
|
||||
itemSaveValidator.validate = jest.fn().mockResolvedValue({ passed: true })
|
||||
|
||||
itemRepository = {} as jest.Mocked<ItemRepositoryInterface>
|
||||
itemRepository.findByUuid = jest.fn().mockResolvedValue(null)
|
||||
|
||||
timer = {} as jest.Mocked<TimerInterface>
|
||||
timer.getTimestampInMicroseconds = jest.fn().mockReturnValue(123)
|
||||
|
||||
savedItem = Item.create({
|
||||
duplicateOf: null,
|
||||
itemsKeyId: 'items-key-id',
|
||||
content: 'content',
|
||||
contentType: ContentType.create(ContentType.TYPES.Note).getValue(),
|
||||
encItemKey: 'enc-item-key',
|
||||
authHash: 'auth-hash',
|
||||
userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
|
||||
deleted: false,
|
||||
updatedWithSession: null,
|
||||
dates: Dates.create(new Date(123), new Date(123)).getValue(),
|
||||
timestamps: Timestamps.create(123, 123).getValue(),
|
||||
}).getValue()
|
||||
|
||||
saveNewItem = {} as jest.Mocked<SaveNewItem>
|
||||
saveNewItem.execute = jest.fn().mockReturnValue(Result.ok(savedItem))
|
||||
|
||||
updateExistingItem = {} as jest.Mocked<UpdateExistingItem>
|
||||
updateExistingItem.execute = jest.fn().mockResolvedValue(Result.ok(savedItem))
|
||||
|
||||
logger = {} as jest.Mocked<Logger>
|
||||
logger.error = jest.fn()
|
||||
|
||||
itemHash1 = ItemHash.create({
|
||||
uuid: 'item-uuid',
|
||||
user_uuid: 'user-uuid',
|
||||
content: 'content',
|
||||
content_type: ContentType.TYPES.Note,
|
||||
deleted: false,
|
||||
auth_hash: 'auth-hash',
|
||||
enc_item_key: 'enc-item-key',
|
||||
items_key_id: 'items-key-id',
|
||||
key_system_identifier: null,
|
||||
shared_vault_uuid: null,
|
||||
created_at: '2020-01-01T00:00:00.000Z',
|
||||
created_at_timestamp: 123,
|
||||
updated_at: '2020-01-01T00:00:00.000Z',
|
||||
updated_at_timestamp: 123,
|
||||
}).getValue()
|
||||
})
|
||||
|
||||
it('should save new items', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
const result = await useCase.execute({
|
||||
itemHashes: [itemHash1],
|
||||
userUuid: 'user-uuid',
|
||||
apiVersion: '1',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue().syncToken).toEqual('MjowLjAwMDEyNA==')
|
||||
expect(saveNewItem.execute).toHaveBeenCalledWith({
|
||||
itemHash: itemHash1,
|
||||
userUuid: 'user-uuid',
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
})
|
||||
|
||||
it('should mark items as conflicts if saving new item fails', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
saveNewItem.execute = jest.fn().mockResolvedValue(Result.fail('error'))
|
||||
|
||||
const result = await useCase.execute({
|
||||
itemHashes: [itemHash1],
|
||||
userUuid: 'user-uuid',
|
||||
apiVersion: '1',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue().conflicts).toEqual([
|
||||
{
|
||||
unsavedItem: itemHash1,
|
||||
type: 'uuid_conflict',
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('should mark items as conflicts if saving new item throws an error', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
saveNewItem.execute = jest.fn().mockRejectedValue(new Error('error'))
|
||||
|
||||
const result = await useCase.execute({
|
||||
itemHashes: [itemHash1],
|
||||
userUuid: 'user-uuid',
|
||||
apiVersion: '1',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue().conflicts).toEqual([
|
||||
{
|
||||
unsavedItem: itemHash1,
|
||||
type: 'uuid_conflict',
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('should not save items if in read-only mode', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
const result = await useCase.execute({
|
||||
itemHashes: [itemHash1],
|
||||
userUuid: 'user-uuid',
|
||||
apiVersion: '1',
|
||||
readOnlyAccess: true,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(saveNewItem.execute).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should return conflicts if the items have not passed validation', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
const conflict = {
|
||||
unsavedItem: itemHash1,
|
||||
type: 'conflict-type',
|
||||
}
|
||||
itemSaveValidator.validate = jest.fn().mockResolvedValue({ passed: false, conflict })
|
||||
|
||||
const result = await useCase.execute({
|
||||
itemHashes: [itemHash1],
|
||||
userUuid: 'user-uuid',
|
||||
apiVersion: '1',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue().conflicts).toEqual([conflict])
|
||||
})
|
||||
|
||||
it('should mark items as saved if they are skipped on validation', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
itemSaveValidator.validate = jest.fn().mockResolvedValue({ passed: false, skipped: savedItem })
|
||||
|
||||
const result = await useCase.execute({
|
||||
itemHashes: [itemHash1],
|
||||
userUuid: 'user-uuid',
|
||||
apiVersion: '1',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue().savedItems).toEqual([savedItem])
|
||||
})
|
||||
|
||||
it('should update existing items', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
itemRepository.findByUuid = jest.fn().mockResolvedValue(savedItem)
|
||||
|
||||
const result = await useCase.execute({
|
||||
itemHashes: [itemHash1],
|
||||
userUuid: 'user-uuid',
|
||||
apiVersion: '1',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(updateExistingItem.execute).toHaveBeenCalledWith({
|
||||
itemHash: itemHash1,
|
||||
existingItem: savedItem,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
})
|
||||
|
||||
it('should mark items as conflicts if updating existing item fails', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
itemRepository.findByUuid = jest.fn().mockResolvedValue(savedItem)
|
||||
updateExistingItem.execute = jest.fn().mockResolvedValue(Result.fail('error'))
|
||||
|
||||
const result = await useCase.execute({
|
||||
itemHashes: [itemHash1],
|
||||
userUuid: 'user-uuid',
|
||||
apiVersion: '1',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue().conflicts).toEqual([
|
||||
{
|
||||
unsavedItem: itemHash1,
|
||||
type: 'uuid_conflict',
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('should calculate the sync token based on existing and new items saved', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
saveNewItem.execute = jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce(Result.ok(savedItem))
|
||||
.mockResolvedValueOnce(
|
||||
Result.ok(
|
||||
Item.create({
|
||||
...savedItem.props,
|
||||
timestamps: Timestamps.create(100, 100).getValue(),
|
||||
}).getValue(),
|
||||
),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
Result.ok(
|
||||
Item.create({
|
||||
...savedItem.props,
|
||||
timestamps: Timestamps.create(159, 159).getValue(),
|
||||
}).getValue(),
|
||||
),
|
||||
)
|
||||
|
||||
const result = await useCase.execute({
|
||||
itemHashes: [
|
||||
itemHash1,
|
||||
ItemHash.create({ ...itemHash1.props, uuid: 'item-uuid-2' }).getValue(),
|
||||
ItemHash.create({ ...itemHash1.props, uuid: 'item-uuid-2' }).getValue(),
|
||||
],
|
||||
userUuid: 'user-uuid',
|
||||
apiVersion: '2',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: 'session-uuid',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeFalsy()
|
||||
expect(result.getValue().syncToken).toEqual('MjowLjAwMDE2')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,145 @@
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
|
||||
import { SaveItemsResult } from './SaveItemsResult'
|
||||
import { SaveItemsDTO } from './SaveItemsDTO'
|
||||
import { Item } from '../../../Item/Item'
|
||||
import { ItemConflict } from '../../../Item/ItemConflict'
|
||||
import { ConflictType } from '@standardnotes/responses'
|
||||
import { Time, TimerInterface } from '@standardnotes/time'
|
||||
import { Logger } from 'winston'
|
||||
import { ItemRepositoryInterface } from '../../../Item/ItemRepositoryInterface'
|
||||
import { ItemSaveValidatorInterface } from '../../../Item/SaveValidator/ItemSaveValidatorInterface'
|
||||
import { SaveNewItem } from '../SaveNewItem/SaveNewItem'
|
||||
import { UpdateExistingItem } from '../UpdateExistingItem/UpdateExistingItem'
|
||||
|
||||
export class SaveItems implements UseCaseInterface<SaveItemsResult> {
|
||||
private readonly SYNC_TOKEN_VERSION = 2
|
||||
|
||||
constructor(
|
||||
private itemSaveValidator: ItemSaveValidatorInterface,
|
||||
private itemRepository: ItemRepositoryInterface,
|
||||
private timer: TimerInterface,
|
||||
private saveNewItem: SaveNewItem,
|
||||
private updateExistingItem: UpdateExistingItem,
|
||||
private logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(dto: SaveItemsDTO): Promise<Result<SaveItemsResult>> {
|
||||
const savedItems: Array<Item> = []
|
||||
const conflicts: Array<ItemConflict> = []
|
||||
|
||||
const lastUpdatedTimestamp = this.timer.getTimestampInMicroseconds()
|
||||
|
||||
for (const itemHash of dto.itemHashes) {
|
||||
if (dto.readOnlyAccess) {
|
||||
conflicts.push({
|
||||
unsavedItem: itemHash,
|
||||
type: ConflictType.ReadOnlyError,
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
const existingItem = await this.itemRepository.findByUuid(itemHash.props.uuid)
|
||||
const processingResult = await this.itemSaveValidator.validate({
|
||||
userUuid: dto.userUuid,
|
||||
apiVersion: dto.apiVersion,
|
||||
itemHash,
|
||||
existingItem,
|
||||
})
|
||||
if (!processingResult.passed) {
|
||||
if (processingResult.conflict) {
|
||||
conflicts.push(processingResult.conflict)
|
||||
}
|
||||
if (processingResult.skipped) {
|
||||
savedItems.push(processingResult.skipped)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if (existingItem) {
|
||||
const udpatedItemOrError = await this.updateExistingItem.execute({
|
||||
existingItem,
|
||||
itemHash,
|
||||
sessionUuid: dto.sessionUuid,
|
||||
})
|
||||
if (udpatedItemOrError.isFailed()) {
|
||||
this.logger.error(
|
||||
`[${dto.userUuid}] Updating item ${itemHash.props.uuid} failed. Error: ${udpatedItemOrError.getError()}`,
|
||||
)
|
||||
|
||||
conflicts.push({
|
||||
unsavedItem: itemHash,
|
||||
type: ConflictType.UuidConflict,
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
const updatedItem = udpatedItemOrError.getValue()
|
||||
|
||||
savedItems.push(updatedItem)
|
||||
} else {
|
||||
try {
|
||||
const newItemOrError = await this.saveNewItem.execute({
|
||||
userUuid: dto.userUuid,
|
||||
itemHash,
|
||||
sessionUuid: dto.sessionUuid,
|
||||
})
|
||||
if (newItemOrError.isFailed()) {
|
||||
this.logger.error(
|
||||
`[${dto.userUuid}] Saving item ${itemHash.props.uuid} failed. Error: ${newItemOrError.getError()}`,
|
||||
)
|
||||
|
||||
conflicts.push({
|
||||
unsavedItem: itemHash,
|
||||
type: ConflictType.UuidConflict,
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
const newItem = newItemOrError.getValue()
|
||||
|
||||
savedItems.push(newItem)
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`[${dto.userUuid}] Saving item ${itemHash.props.uuid} failed. Error: ${(error as Error).message}`,
|
||||
)
|
||||
|
||||
conflicts.push({
|
||||
unsavedItem: itemHash,
|
||||
type: ConflictType.UuidConflict,
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const syncToken = this.calculateSyncToken(lastUpdatedTimestamp, savedItems)
|
||||
|
||||
return Result.ok({
|
||||
savedItems,
|
||||
conflicts,
|
||||
syncToken,
|
||||
})
|
||||
}
|
||||
|
||||
private calculateSyncToken(lastUpdatedTimestamp: number, savedItems: Array<Item>): string {
|
||||
if (savedItems.length) {
|
||||
const sortedItems = savedItems.sort((itemA: Item, itemB: Item) => {
|
||||
return itemA.props.timestamps.updatedAt > itemB.props.timestamps.updatedAt ? 1 : -1
|
||||
})
|
||||
lastUpdatedTimestamp = sortedItems[sortedItems.length - 1].props.timestamps.updatedAt
|
||||
}
|
||||
|
||||
const lastUpdatedTimestampWithMicrosecondPreventingSyncDoubles = lastUpdatedTimestamp + 1
|
||||
|
||||
return Buffer.from(
|
||||
`${this.SYNC_TOKEN_VERSION}:${
|
||||
lastUpdatedTimestampWithMicrosecondPreventingSyncDoubles / Time.MicrosecondsInASecond
|
||||
}`,
|
||||
'utf-8',
|
||||
).toString('base64')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { ItemHash } from '../../../Item/ItemHash'
|
||||
|
||||
export interface SaveItemsDTO {
|
||||
itemHashes: ItemHash[]
|
||||
userUuid: string
|
||||
apiVersion: string
|
||||
readOnlyAccess: boolean
|
||||
sessionUuid: string | null
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Item } from '../../../Item/Item'
|
||||
import { ItemConflict } from '../../../Item/ItemConflict'
|
||||
|
||||
export interface SaveItemsResult {
|
||||
savedItems: Item[]
|
||||
conflicts: ItemConflict[]
|
||||
syncToken: string
|
||||
}
|
||||
+33
-12
@@ -37,8 +37,11 @@ describe('SaveNewItem', () => {
|
||||
new UniqueEntityId('00000000-0000-0000-0000-000000000000'),
|
||||
).getValue()
|
||||
|
||||
itemHash1 = {
|
||||
itemHash1 = ItemHash.create({
|
||||
uuid: '1-2-3',
|
||||
user_uuid: '00000000-0000-0000-0000-000000000000',
|
||||
key_system_identifier: null,
|
||||
shared_vault_uuid: null,
|
||||
content: 'asdqwe1',
|
||||
content_type: ContentType.TYPES.Note,
|
||||
duplicate_of: null,
|
||||
@@ -52,7 +55,7 @@ describe('SaveNewItem', () => {
|
||||
new Date(timeHelper.convertMicrosecondsToMilliseconds(item1.props.timestamps.updatedAt) + 1),
|
||||
'YYYY-MM-DDTHH:mm:ss.SSS[Z]',
|
||||
),
|
||||
} as jest.Mocked<ItemHash>
|
||||
}).getValue()
|
||||
|
||||
itemRepository = {} as jest.Mocked<ItemRepositoryInterface>
|
||||
itemRepository.save = jest.fn()
|
||||
@@ -91,10 +94,13 @@ describe('SaveNewItem', () => {
|
||||
it('saves a new empty item', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
itemHash1.content = undefined
|
||||
itemHash1.content_type = null
|
||||
itemHash1.enc_item_key = undefined
|
||||
itemHash1.items_key_id = undefined
|
||||
itemHash1 = ItemHash.create({
|
||||
...itemHash1.props,
|
||||
content: undefined,
|
||||
content_type: null,
|
||||
enc_item_key: undefined,
|
||||
items_key_id: undefined,
|
||||
}).getValue()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: '00000000-0000-0000-0000-000000000000',
|
||||
@@ -109,8 +115,11 @@ describe('SaveNewItem', () => {
|
||||
it('saves a new item with given timestamps', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
itemHash1.created_at_timestamp = 123
|
||||
itemHash1.updated_at_timestamp = 123
|
||||
itemHash1 = ItemHash.create({
|
||||
...itemHash1.props,
|
||||
created_at_timestamp: 123,
|
||||
updated_at_timestamp: 123,
|
||||
}).getValue()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: '00000000-0000-0000-0000-000000000000',
|
||||
@@ -124,7 +133,10 @@ describe('SaveNewItem', () => {
|
||||
it('publishes a duplicate item synced event if the item is a duplicate', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
itemHash1.duplicate_of = '00000000-0000-0000-0000-000000000003'
|
||||
itemHash1 = ItemHash.create({
|
||||
...itemHash1.props,
|
||||
duplicate_of: '00000000-0000-0000-0000-000000000003',
|
||||
}).getValue()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: '00000000-0000-0000-0000-000000000000',
|
||||
@@ -140,7 +152,10 @@ describe('SaveNewItem', () => {
|
||||
it('publishes a item revision creation requested event if the item is a revision', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
itemHash1.updated_at = '2021-03-19T17:17:13.241Z'
|
||||
itemHash1 = ItemHash.create({
|
||||
...itemHash1.props,
|
||||
updated_at: '2021-03-19T17:17:13.241Z',
|
||||
}).getValue()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: '00000000-0000-0000-0000-000000000000',
|
||||
@@ -199,7 +214,10 @@ describe('SaveNewItem', () => {
|
||||
it('returns a failure if the content type is invalid', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
itemHash1.content_type = 'invalid'
|
||||
itemHash1 = ItemHash.create({
|
||||
...itemHash1.props,
|
||||
content_type: 'invalid',
|
||||
}).getValue()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: '00000000-0000-0000-0000-000000000000',
|
||||
@@ -213,7 +231,10 @@ describe('SaveNewItem', () => {
|
||||
it('returns a failure if the duplicate uuid is invalid', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
itemHash1.duplicate_of = 'invalid'
|
||||
itemHash1 = ItemHash.create({
|
||||
...itemHash1.props,
|
||||
duplicate_of: 'invalid',
|
||||
}).getValue()
|
||||
|
||||
const result = await useCase.execute({
|
||||
userUuid: '00000000-0000-0000-0000-000000000000',
|
||||
|
||||
@@ -38,15 +38,15 @@ export class SaveNewItem implements UseCaseInterface<Item> {
|
||||
}
|
||||
const userUuid = userUuidOrError.getValue()
|
||||
|
||||
const contentTypeOrError = ContentType.create(dto.itemHash.content_type)
|
||||
const contentTypeOrError = ContentType.create(dto.itemHash.props.content_type)
|
||||
if (contentTypeOrError.isFailed()) {
|
||||
return Result.fail(contentTypeOrError.getError())
|
||||
}
|
||||
const contentType = contentTypeOrError.getValue()
|
||||
|
||||
let duplicateOf = null
|
||||
if (dto.itemHash.duplicate_of) {
|
||||
const duplicateOfOrError = Uuid.create(dto.itemHash.duplicate_of)
|
||||
if (dto.itemHash.props.duplicate_of) {
|
||||
const duplicateOfOrError = Uuid.create(dto.itemHash.props.duplicate_of)
|
||||
if (duplicateOfOrError.isFailed()) {
|
||||
return Result.fail(duplicateOfOrError.getError())
|
||||
}
|
||||
@@ -58,12 +58,12 @@ export class SaveNewItem implements UseCaseInterface<Item> {
|
||||
|
||||
let createdAtDate = nowDate
|
||||
let createdAtTimestamp = now
|
||||
if (dto.itemHash.created_at_timestamp) {
|
||||
createdAtTimestamp = dto.itemHash.created_at_timestamp
|
||||
if (dto.itemHash.props.created_at_timestamp) {
|
||||
createdAtTimestamp = dto.itemHash.props.created_at_timestamp
|
||||
createdAtDate = this.timer.convertMicrosecondsToDate(createdAtTimestamp)
|
||||
} else if (dto.itemHash.created_at) {
|
||||
createdAtTimestamp = this.timer.convertStringDateToMicroseconds(dto.itemHash.created_at)
|
||||
createdAtDate = this.timer.convertStringDateToDate(dto.itemHash.created_at)
|
||||
} 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)
|
||||
}
|
||||
|
||||
const datesOrError = Dates.create(createdAtDate, nowDate)
|
||||
@@ -81,18 +81,18 @@ export class SaveNewItem implements UseCaseInterface<Item> {
|
||||
const itemOrError = Item.create(
|
||||
{
|
||||
updatedWithSession,
|
||||
content: dto.itemHash.content ?? null,
|
||||
content: dto.itemHash.props.content ?? null,
|
||||
userUuid,
|
||||
contentType,
|
||||
encItemKey: dto.itemHash.enc_item_key ?? null,
|
||||
authHash: dto.itemHash.auth_hash ?? null,
|
||||
itemsKeyId: dto.itemHash.items_key_id ?? null,
|
||||
encItemKey: dto.itemHash.props.enc_item_key ?? null,
|
||||
authHash: dto.itemHash.props.auth_hash ?? null,
|
||||
itemsKeyId: dto.itemHash.props.items_key_id ?? null,
|
||||
duplicateOf,
|
||||
deleted: dto.itemHash.deleted ?? false,
|
||||
deleted: dto.itemHash.props.deleted ?? false,
|
||||
dates,
|
||||
timestamps,
|
||||
},
|
||||
new UniqueEntityId(dto.itemHash.uuid),
|
||||
new UniqueEntityId(dto.itemHash.props.uuid),
|
||||
)
|
||||
if (itemOrError.isFailed()) {
|
||||
return Result.fail(itemOrError.getError())
|
||||
|
||||
@@ -3,19 +3,23 @@ import 'reflect-metadata'
|
||||
import { ApiVersion } from '../../../Api/ApiVersion'
|
||||
import { Item } from '../../../Item/Item'
|
||||
import { ItemHash } from '../../../Item/ItemHash'
|
||||
import { ItemServiceInterface } from '../../../Item/ItemServiceInterface'
|
||||
|
||||
import { SyncItems } from './SyncItems'
|
||||
import { ContentType, Dates, Timestamps, UniqueEntityId, Uuid } from '@standardnotes/domain-core'
|
||||
import { ContentType, Dates, Result, Timestamps, UniqueEntityId, Uuid } from '@standardnotes/domain-core'
|
||||
import { GetItems } from '../GetItems/GetItems'
|
||||
import { SaveItems } from '../SaveItems/SaveItems'
|
||||
import { ItemRepositoryInterface } from '../../../Item/ItemRepositoryInterface'
|
||||
|
||||
describe('SyncItems', () => {
|
||||
let itemService: ItemServiceInterface
|
||||
let getItemsUseCase: GetItems
|
||||
let saveItemsUseCase: SaveItems
|
||||
let itemRepository: ItemRepositoryInterface
|
||||
let item1: Item
|
||||
let item2: Item
|
||||
let item3: Item
|
||||
let itemHash: ItemHash
|
||||
|
||||
const createUseCase = () => new SyncItems(itemService)
|
||||
const createUseCase = () => new SyncItems(itemRepository, getItemsUseCase, saveItemsUseCase)
|
||||
|
||||
beforeEach(() => {
|
||||
item1 = Item.create(
|
||||
@@ -67,8 +71,11 @@ describe('SyncItems', () => {
|
||||
new UniqueEntityId('00000000-0000-0000-0000-000000000003'),
|
||||
).getValue()
|
||||
|
||||
itemHash = {
|
||||
itemHash = ItemHash.create({
|
||||
uuid: '2-3-4',
|
||||
user_uuid: '1-2-3',
|
||||
key_system_identifier: null,
|
||||
shared_vault_uuid: null,
|
||||
content: 'asdqwe',
|
||||
content_type: ContentType.TYPES.Note,
|
||||
duplicate_of: null,
|
||||
@@ -76,19 +83,27 @@ describe('SyncItems', () => {
|
||||
items_key_id: 'asdasd',
|
||||
created_at: '2021-02-19T11:35:45.655Z',
|
||||
updated_at: '2021-03-25T09:37:37.944Z',
|
||||
}
|
||||
}).getValue()
|
||||
|
||||
itemService = {} as jest.Mocked<ItemServiceInterface>
|
||||
itemService.getItems = jest.fn().mockReturnValue({
|
||||
items: [item1],
|
||||
cursorToken: 'asdzxc',
|
||||
})
|
||||
itemService.saveItems = jest.fn().mockReturnValue({
|
||||
savedItems: [item2],
|
||||
conflicts: [],
|
||||
syncToken: 'qwerty',
|
||||
})
|
||||
itemService.frontLoadKeysItemsToTop = jest.fn().mockReturnValue([item3, item1])
|
||||
getItemsUseCase = {} as jest.Mocked<GetItems>
|
||||
getItemsUseCase.execute = jest.fn().mockReturnValue(
|
||||
Result.ok({
|
||||
items: [item1],
|
||||
cursorToken: 'asdzxc',
|
||||
}),
|
||||
)
|
||||
|
||||
saveItemsUseCase = {} as jest.Mocked<SaveItems>
|
||||
saveItemsUseCase.execute = jest.fn().mockReturnValue(
|
||||
Result.ok({
|
||||
savedItems: [item2],
|
||||
conflicts: [],
|
||||
syncToken: 'qwerty',
|
||||
}),
|
||||
)
|
||||
|
||||
itemRepository = {} as jest.Mocked<ItemRepositoryInterface>
|
||||
itemRepository.findAll = jest.fn().mockReturnValue([item3, item1])
|
||||
})
|
||||
|
||||
it('should sync items', async () => {
|
||||
@@ -113,15 +128,14 @@ describe('SyncItems', () => {
|
||||
syncToken: 'qwerty',
|
||||
})
|
||||
|
||||
expect(itemService.frontLoadKeysItemsToTop).not.toHaveBeenCalled()
|
||||
expect(itemService.getItems).toHaveBeenCalledWith({
|
||||
expect(getItemsUseCase.execute).toHaveBeenCalledWith({
|
||||
contentType: 'Note',
|
||||
cursorToken: 'bar',
|
||||
limit: 10,
|
||||
syncToken: 'foo',
|
||||
userUuid: '1-2-3',
|
||||
})
|
||||
expect(itemService.saveItems).toHaveBeenCalledWith({
|
||||
expect(saveItemsUseCase.execute).toHaveBeenCalledWith({
|
||||
itemHashes: [itemHash],
|
||||
userUuid: '1-2-3',
|
||||
apiVersion: '20200115',
|
||||
@@ -152,25 +166,29 @@ describe('SyncItems', () => {
|
||||
})
|
||||
|
||||
it('should sync items and return filtered out sync conflicts for consecutive sync operations', async () => {
|
||||
itemService.getItems = jest.fn().mockReturnValue({
|
||||
items: [item1, item2],
|
||||
cursorToken: 'asdzxc',
|
||||
})
|
||||
getItemsUseCase.execute = jest.fn().mockReturnValue(
|
||||
Result.ok({
|
||||
items: [item1, item2],
|
||||
cursorToken: 'asdzxc',
|
||||
}),
|
||||
)
|
||||
|
||||
itemService.saveItems = jest.fn().mockReturnValue({
|
||||
savedItems: [],
|
||||
conflicts: [
|
||||
{
|
||||
serverItem: item2,
|
||||
type: 'sync_conflict',
|
||||
},
|
||||
{
|
||||
serverItem: undefined,
|
||||
type: 'sync_conflict',
|
||||
},
|
||||
],
|
||||
syncToken: 'qwerty',
|
||||
})
|
||||
saveItemsUseCase.execute = jest.fn().mockReturnValue(
|
||||
Result.ok({
|
||||
savedItems: [],
|
||||
conflicts: [
|
||||
{
|
||||
serverItem: item2,
|
||||
type: 'sync_conflict',
|
||||
},
|
||||
{
|
||||
serverItem: undefined,
|
||||
type: 'sync_conflict',
|
||||
},
|
||||
],
|
||||
syncToken: 'qwerty',
|
||||
}),
|
||||
)
|
||||
|
||||
const result = await createUseCase().execute({
|
||||
userUuid: '1-2-3',
|
||||
@@ -203,4 +221,44 @@ describe('SyncItems', () => {
|
||||
syncToken: 'qwerty',
|
||||
})
|
||||
})
|
||||
|
||||
it('should return error if get items fails', async () => {
|
||||
getItemsUseCase.execute = jest.fn().mockReturnValue(Result.fail('error'))
|
||||
|
||||
const result = await createUseCase().execute({
|
||||
userUuid: '1-2-3',
|
||||
itemHashes: [itemHash],
|
||||
computeIntegrityHash: false,
|
||||
syncToken: 'foo',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: '2-3-4',
|
||||
cursorToken: 'bar',
|
||||
limit: 10,
|
||||
contentType: 'Note',
|
||||
apiVersion: ApiVersion.v20200115,
|
||||
snjsVersion: '1.2.3',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should return error if save items fails', async () => {
|
||||
saveItemsUseCase.execute = jest.fn().mockReturnValue(Result.fail('error'))
|
||||
|
||||
const result = await createUseCase().execute({
|
||||
userUuid: '1-2-3',
|
||||
itemHashes: [itemHash],
|
||||
computeIntegrityHash: false,
|
||||
syncToken: 'foo',
|
||||
readOnlyAccess: false,
|
||||
sessionUuid: '2-3-4',
|
||||
cursorToken: 'bar',
|
||||
limit: 10,
|
||||
contentType: 'Note',
|
||||
apiVersion: ApiVersion.v20200115,
|
||||
snjsVersion: '1.2.3',
|
||||
})
|
||||
|
||||
expect(result.isFailed()).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,34 +1,48 @@
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
import { ContentType, Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
|
||||
import { Item } from '../../../Item/Item'
|
||||
import { ItemConflict } from '../../../Item/ItemConflict'
|
||||
import { ItemServiceInterface } from '../../../Item/ItemServiceInterface'
|
||||
import { SyncItemsDTO } from './SyncItemsDTO'
|
||||
import { SyncItemsResponse } from './SyncItemsResponse'
|
||||
import { ItemRepositoryInterface } from '../../../Item/ItemRepositoryInterface'
|
||||
import { GetItems } from '../GetItems/GetItems'
|
||||
import { SaveItems } from '../SaveItems/SaveItems'
|
||||
|
||||
export class SyncItems implements UseCaseInterface<SyncItemsResponse> {
|
||||
constructor(private itemService: ItemServiceInterface) {}
|
||||
constructor(
|
||||
private itemRepository: ItemRepositoryInterface,
|
||||
private getItemsUseCase: GetItems,
|
||||
private saveItemsUseCase: SaveItems,
|
||||
) {}
|
||||
|
||||
async execute(dto: SyncItemsDTO): Promise<Result<SyncItemsResponse>> {
|
||||
const getItemsResult = await this.itemService.getItems({
|
||||
const getItemsResultOrError = await this.getItemsUseCase.execute({
|
||||
userUuid: dto.userUuid,
|
||||
syncToken: dto.syncToken,
|
||||
cursorToken: dto.cursorToken,
|
||||
limit: dto.limit,
|
||||
contentType: dto.contentType,
|
||||
})
|
||||
if (getItemsResultOrError.isFailed()) {
|
||||
return Result.fail(getItemsResultOrError.getError())
|
||||
}
|
||||
const getItemsResult = getItemsResultOrError.getValue()
|
||||
|
||||
const saveItemsResult = await this.itemService.saveItems({
|
||||
const saveItemsResultOrError = await this.saveItemsUseCase.execute({
|
||||
itemHashes: dto.itemHashes,
|
||||
userUuid: dto.userUuid,
|
||||
apiVersion: dto.apiVersion,
|
||||
readOnlyAccess: dto.readOnlyAccess,
|
||||
sessionUuid: dto.sessionUuid,
|
||||
})
|
||||
if (saveItemsResultOrError.isFailed()) {
|
||||
return Result.fail(saveItemsResultOrError.getError())
|
||||
}
|
||||
const saveItemsResult = saveItemsResultOrError.getValue()
|
||||
|
||||
let retrievedItems = this.filterOutSyncConflictsForConsecutiveSyncs(getItemsResult.items, saveItemsResult.conflicts)
|
||||
if (this.isFirstSync(dto)) {
|
||||
retrievedItems = await this.itemService.frontLoadKeysItemsToTop(dto.userUuid, retrievedItems)
|
||||
retrievedItems = await this.frontLoadKeysItemsToTop(dto.userUuid, retrievedItems)
|
||||
}
|
||||
|
||||
const syncResponse: SyncItemsResponse = {
|
||||
@@ -59,4 +73,23 @@ export class SyncItems implements UseCaseInterface<SyncItemsResponse> {
|
||||
|
||||
return retrievedItems.filter((item: Item) => syncConflictIds.indexOf(item.id.toString()) === -1)
|
||||
}
|
||||
|
||||
private async frontLoadKeysItemsToTop(userUuid: string, retrievedItems: Array<Item>): Promise<Array<Item>> {
|
||||
const itemsKeys = await this.itemRepository.findAll({
|
||||
userUuid,
|
||||
contentType: ContentType.TYPES.ItemsKey,
|
||||
sortBy: 'updated_at_timestamp',
|
||||
sortOrder: 'ASC',
|
||||
})
|
||||
|
||||
const retrievedItemsIds: Array<string> = retrievedItems.map((item: Item) => item.id.toString())
|
||||
|
||||
itemsKeys.forEach((itemKey: Item) => {
|
||||
if (retrievedItemsIds.indexOf(itemKey.id.toString()) === -1) {
|
||||
retrievedItems.unshift(itemKey)
|
||||
}
|
||||
})
|
||||
|
||||
return retrievedItems
|
||||
}
|
||||
}
|
||||
|
||||
+29
-26
@@ -37,8 +37,11 @@ describe('UpdateExistingItem', () => {
|
||||
new UniqueEntityId('00000000-0000-0000-0000-000000000000'),
|
||||
).getValue()
|
||||
|
||||
itemHash1 = {
|
||||
itemHash1 = ItemHash.create({
|
||||
uuid: '1-2-3',
|
||||
user_uuid: '00000000-0000-0000-0000-000000000000',
|
||||
key_system_identifier: null,
|
||||
shared_vault_uuid: null,
|
||||
content: 'asdqwe1',
|
||||
content_type: ContentType.TYPES.Note,
|
||||
duplicate_of: null,
|
||||
@@ -53,7 +56,7 @@ describe('UpdateExistingItem', () => {
|
||||
new Date(timeHelper.convertMicrosecondsToMilliseconds(item1.props.timestamps.updatedAt) + 1),
|
||||
'YYYY-MM-DDTHH:mm:ss.SSS[Z]',
|
||||
),
|
||||
} as jest.Mocked<ItemHash>
|
||||
}).getValue()
|
||||
|
||||
itemRepository = {} as jest.Mocked<ItemRepositoryInterface>
|
||||
itemRepository.save = jest.fn()
|
||||
@@ -107,10 +110,10 @@ describe('UpdateExistingItem', () => {
|
||||
|
||||
const result = await useCase.execute({
|
||||
existingItem: item1,
|
||||
itemHash: {
|
||||
...itemHash1,
|
||||
itemHash: ItemHash.create({
|
||||
...itemHash1.props,
|
||||
content_type: 'invalid',
|
||||
},
|
||||
}).getValue(),
|
||||
sessionUuid: '00000000-0000-0000-0000-000000000000',
|
||||
})
|
||||
|
||||
@@ -122,10 +125,10 @@ describe('UpdateExistingItem', () => {
|
||||
|
||||
const result = await useCase.execute({
|
||||
existingItem: item1,
|
||||
itemHash: {
|
||||
...itemHash1,
|
||||
itemHash: ItemHash.create({
|
||||
...itemHash1.props,
|
||||
deleted: true,
|
||||
},
|
||||
}).getValue(),
|
||||
sessionUuid: '00000000-0000-0000-0000-000000000000',
|
||||
})
|
||||
|
||||
@@ -144,10 +147,10 @@ describe('UpdateExistingItem', () => {
|
||||
|
||||
const result = await useCase.execute({
|
||||
existingItem: item1,
|
||||
itemHash: {
|
||||
...itemHash1,
|
||||
itemHash: ItemHash.create({
|
||||
...itemHash1.props,
|
||||
duplicate_of: '00000000-0000-0000-0000-000000000001',
|
||||
},
|
||||
}).getValue(),
|
||||
sessionUuid: '00000000-0000-0000-0000-000000000000',
|
||||
})
|
||||
|
||||
@@ -161,10 +164,10 @@ describe('UpdateExistingItem', () => {
|
||||
|
||||
const result = await useCase.execute({
|
||||
existingItem: item1,
|
||||
itemHash: {
|
||||
...itemHash1,
|
||||
itemHash: ItemHash.create({
|
||||
...itemHash1.props,
|
||||
duplicate_of: 'invalid-uuid',
|
||||
},
|
||||
}).getValue(),
|
||||
sessionUuid: '00000000-0000-0000-0000-000000000000',
|
||||
})
|
||||
|
||||
@@ -176,11 +179,11 @@ describe('UpdateExistingItem', () => {
|
||||
|
||||
const result = await useCase.execute({
|
||||
existingItem: item1,
|
||||
itemHash: {
|
||||
...itemHash1,
|
||||
itemHash: ItemHash.create({
|
||||
...itemHash1.props,
|
||||
updated_at_timestamp: 123,
|
||||
created_at_timestamp: 123,
|
||||
},
|
||||
}).getValue(),
|
||||
sessionUuid: '00000000-0000-0000-0000-000000000000',
|
||||
})
|
||||
|
||||
@@ -193,11 +196,11 @@ describe('UpdateExistingItem', () => {
|
||||
|
||||
const result = await useCase.execute({
|
||||
existingItem: item1,
|
||||
itemHash: {
|
||||
...itemHash1,
|
||||
itemHash: ItemHash.create({
|
||||
...itemHash1.props,
|
||||
created_at: undefined,
|
||||
created_at_timestamp: undefined,
|
||||
},
|
||||
}).getValue(),
|
||||
sessionUuid: '00000000-0000-0000-0000-000000000000',
|
||||
})
|
||||
|
||||
@@ -214,11 +217,11 @@ describe('UpdateExistingItem', () => {
|
||||
|
||||
const result = await useCase.execute({
|
||||
existingItem: item1,
|
||||
itemHash: {
|
||||
...itemHash1,
|
||||
itemHash: ItemHash.create({
|
||||
...itemHash1.props,
|
||||
created_at_timestamp: 123,
|
||||
updated_at_timestamp: 123,
|
||||
},
|
||||
}).getValue(),
|
||||
sessionUuid: '00000000-0000-0000-0000-000000000000',
|
||||
})
|
||||
|
||||
@@ -237,11 +240,11 @@ describe('UpdateExistingItem', () => {
|
||||
|
||||
const result = await useCase.execute({
|
||||
existingItem: item1,
|
||||
itemHash: {
|
||||
...itemHash1,
|
||||
itemHash: ItemHash.create({
|
||||
...itemHash1.props,
|
||||
created_at_timestamp: 123,
|
||||
updated_at_timestamp: 123,
|
||||
},
|
||||
}).getValue(),
|
||||
sessionUuid: '00000000-0000-0000-0000-000000000000',
|
||||
})
|
||||
|
||||
|
||||
+20
-20
@@ -27,12 +27,12 @@ export class UpdateExistingItem implements UseCaseInterface<Item> {
|
||||
}
|
||||
dto.existingItem.props.updatedWithSession = sessionUuid
|
||||
|
||||
if (dto.itemHash.content) {
|
||||
dto.existingItem.props.content = dto.itemHash.content
|
||||
if (dto.itemHash.props.content) {
|
||||
dto.existingItem.props.content = dto.itemHash.props.content
|
||||
}
|
||||
|
||||
if (dto.itemHash.content_type) {
|
||||
const contentTypeOrError = ContentType.create(dto.itemHash.content_type)
|
||||
if (dto.itemHash.props.content_type) {
|
||||
const contentTypeOrError = ContentType.create(dto.itemHash.props.content_type)
|
||||
if (contentTypeOrError.isFailed()) {
|
||||
return Result.fail(contentTypeOrError.getError())
|
||||
}
|
||||
@@ -40,13 +40,13 @@ export class UpdateExistingItem implements UseCaseInterface<Item> {
|
||||
dto.existingItem.props.contentType = contentType
|
||||
}
|
||||
|
||||
if (dto.itemHash.deleted !== undefined) {
|
||||
dto.existingItem.props.deleted = dto.itemHash.deleted
|
||||
if (dto.itemHash.props.deleted !== undefined) {
|
||||
dto.existingItem.props.deleted = dto.itemHash.props.deleted
|
||||
}
|
||||
|
||||
let wasMarkedAsDuplicate = false
|
||||
if (dto.itemHash.duplicate_of) {
|
||||
const duplicateOfOrError = Uuid.create(dto.itemHash.duplicate_of)
|
||||
if (dto.itemHash.props.duplicate_of) {
|
||||
const duplicateOfOrError = Uuid.create(dto.itemHash.props.duplicate_of)
|
||||
if (duplicateOfOrError.isFailed()) {
|
||||
return Result.fail(duplicateOfOrError.getError())
|
||||
}
|
||||
@@ -54,14 +54,14 @@ export class UpdateExistingItem implements UseCaseInterface<Item> {
|
||||
dto.existingItem.props.duplicateOf = duplicateOfOrError.getValue()
|
||||
}
|
||||
|
||||
if (dto.itemHash.auth_hash) {
|
||||
dto.existingItem.props.authHash = dto.itemHash.auth_hash
|
||||
if (dto.itemHash.props.auth_hash) {
|
||||
dto.existingItem.props.authHash = dto.itemHash.props.auth_hash
|
||||
}
|
||||
if (dto.itemHash.enc_item_key) {
|
||||
dto.existingItem.props.encItemKey = dto.itemHash.enc_item_key
|
||||
if (dto.itemHash.props.enc_item_key) {
|
||||
dto.existingItem.props.encItemKey = dto.itemHash.props.enc_item_key
|
||||
}
|
||||
if (dto.itemHash.items_key_id) {
|
||||
dto.existingItem.props.itemsKeyId = dto.itemHash.items_key_id
|
||||
if (dto.itemHash.props.items_key_id) {
|
||||
dto.existingItem.props.itemsKeyId = dto.itemHash.props.items_key_id
|
||||
}
|
||||
|
||||
const updatedAtTimestamp = this.timer.getTimestampInMicroseconds()
|
||||
@@ -72,12 +72,12 @@ export class UpdateExistingItem implements UseCaseInterface<Item> {
|
||||
|
||||
let createdAtTimestamp: number
|
||||
let createdAtDate: Date
|
||||
if (dto.itemHash.created_at_timestamp) {
|
||||
createdAtTimestamp = dto.itemHash.created_at_timestamp
|
||||
if (dto.itemHash.props.created_at_timestamp) {
|
||||
createdAtTimestamp = dto.itemHash.props.created_at_timestamp
|
||||
createdAtDate = this.timer.convertMicrosecondsToDate(createdAtTimestamp)
|
||||
} else if (dto.itemHash.created_at) {
|
||||
createdAtTimestamp = this.timer.convertStringDateToMicroseconds(dto.itemHash.created_at)
|
||||
createdAtDate = this.timer.convertStringDateToDate(dto.itemHash.created_at)
|
||||
} 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.')
|
||||
}
|
||||
@@ -96,7 +96,7 @@ export class UpdateExistingItem implements UseCaseInterface<Item> {
|
||||
|
||||
dto.existingItem.props.contentSize = Buffer.byteLength(JSON.stringify(dto.existingItem))
|
||||
|
||||
if (dto.itemHash.deleted === true) {
|
||||
if (dto.itemHash.props.deleted === true) {
|
||||
dto.existingItem.props.deleted = true
|
||||
dto.existingItem.props.content = null
|
||||
dto.existingItem.props.contentSize = 0
|
||||
|
||||
Reference in New Issue
Block a user