fix: unify use case usage (#654)

This commit is contained in:
Karol Sójko
2023-07-11 17:05:45 +02:00
committed by GitHub
parent 108408a944
commit 4d1e2dec26
13 changed files with 168 additions and 191 deletions
@@ -10,6 +10,7 @@ import { ItemProjection } from '../../../Projection/ItemProjection'
import { ProjectorInterface } from '../../../Projection/ProjectorInterface'
import { ApiVersion } from '../../../Domain/Api/ApiVersion'
import { SyncItems } from '../../../Domain/UseCase/Syncing/SyncItems/SyncItems'
import { HttpStatusCode } from '@standardnotes/responses'
export class HomeServerItemsController extends BaseHttpController {
constructor(
@@ -41,16 +42,21 @@ export class HomeServerItemsController extends BaseHttpController {
computeIntegrityHash: request.body.compute_integrity === true,
syncToken: request.body.sync_token,
cursorToken: request.body.cursor_token,
sharedVaultUuids: request.body.shared_vault_uuids,
limit: request.body.limit,
contentType: request.body.content_type,
apiVersion: request.body.api ?? ApiVersion.v20161215,
snjsVersion: <string>request.headers['x-snjs-version'],
readOnlyAccess: response.locals.readOnlyAccess,
sessionUuid: response.locals.session ? response.locals.session.uuid : null,
})
if (syncResult.isFailed()) {
return this.json({ error: { message: syncResult.getError() } }, HttpStatusCode.BadRequest)
}
const syncResponse = await this.syncResponseFactoryResolver
.resolveSyncResponseFactoryVersion(request.body.api)
.createResponse(syncResult)
.createResponse(syncResult.getValue())
return this.json(syncResponse)
}
@@ -67,19 +73,25 @@ export class HomeServerItemsController extends BaseHttpController {
freeUser: response.locals.freeUser,
})
return this.json(result)
if (result.isFailed()) {
return this.json({ error: { message: result.getError() } }, HttpStatusCode.BadRequest)
}
return this.json({
mismatches: result.getValue(),
})
}
async getSingleItem(request: Request, response: Response): Promise<results.NotFoundResult | results.JsonResult> {
async getSingleItem(request: Request, response: Response): Promise<results.JsonResult> {
const result = await this.getItem.execute({
userUuid: response.locals.user.uuid,
itemUuid: request.params.uuid,
})
if (!result.success) {
return this.notFound()
if (result.isFailed()) {
return this.json({ error: { message: result.getError() } }, 404)
}
return this.json({ item: await this.itemProjector.projectFull(result.item) })
return this.json({ item: await this.itemProjector.projectFull(result.getValue()) })
}
}
@@ -2,9 +2,10 @@ import 'reflect-metadata'
import * as express from 'express'
import { ContentType } from '@standardnotes/common'
import { Result } from '@standardnotes/domain-core'
import { results } from 'inversify-express-utils'
import { InversifyExpressItemsController } from './InversifyExpressItemsController'
import { results } from 'inversify-express-utils'
import { Item } from '../../Domain/Item/Item'
import { ItemProjection } from '../../Projection/ItemProjection'
import { ProjectorInterface } from '../../Projection/ProjectorInterface'
@@ -35,13 +36,13 @@ describe('InversifyExpressItemsController', () => {
itemProjector.projectFull = jest.fn().mockReturnValue({ foo: 'bar' })
syncItems = {} as jest.Mocked<SyncItems>
syncItems.execute = jest.fn().mockReturnValue({ foo: 'bar' })
syncItems.execute = jest.fn().mockReturnValue(Result.ok({ foo: 'bar' }))
checkIntegrity = {} as jest.Mocked<CheckIntegrity>
checkIntegrity.execute = jest.fn().mockReturnValue({ mismatches: [{ uuid: '1-2-3', updated_at_timestamp: 2 }] })
checkIntegrity.execute = jest.fn().mockReturnValue(Result.ok([{ uuid: '1-2-3', updated_at_timestamp: 2 }]))
getItem = {} as jest.Mocked<GetItem>
getItem.execute = jest.fn().mockReturnValue({ success: true, item: {} as jest.Mocked<Item> })
getItem.execute = jest.fn().mockReturnValue(Result.ok({} as jest.Mocked<Item>))
request = {
headers: {},
@@ -100,7 +101,7 @@ describe('InversifyExpressItemsController', () => {
it('should return 404 on a missing single item', async () => {
request.params.uuid = '1-2-3'
getItem.execute = jest.fn().mockReturnValue({ success: false })
getItem.execute = jest.fn().mockReturnValue(Result.fail('Oops'))
const httpResponse = <results.NotFoundResult>await createController().getSingleItem(request, response)
const result = await httpResponse.executeAsync()
@@ -36,10 +36,7 @@ export class InversifyExpressItemsController extends HomeServerItemsController {
}
@httpGet('/:uuid')
override async getSingleItem(
request: Request,
response: Response,
): Promise<results.NotFoundResult | results.JsonResult> {
override async getSingleItem(request: Request, response: Response): Promise<results.JsonResult> {
return super.getSingleItem(request, response)
}
}