Skip to content

Commit

Permalink
remove fp-ts from controller
Browse files Browse the repository at this point in the history
  • Loading branch information
TIL-EBP committed Oct 30, 2024
1 parent c8ff38e commit 6471094
Showing 1 changed file with 9 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNotNull, unknownToUnknownError } from '@asset-sg/core';
import { AssetEditDetail, PatchAsset } from '@asset-sg/shared';
import { AssetEditPolicy, Role, User } from '@asset-sg/shared/v2';
import {
Expand All @@ -13,15 +12,11 @@ import {
Post,
Put,
} from '@nestjs/common';
import * as E from 'fp-ts/Either';
import { pipe } from 'fp-ts/function';
import * as TE from 'fp-ts/TaskEither';
import { authorize } from '@/core/authorize';
import { CurrentUser } from '@/core/decorators/current-user.decorator';
import { ParseBody } from '@/core/decorators/parse.decorator';
import { AssetEditRepo } from '@/features/asset-edit/asset-edit.repo';
import { AssetSearchService } from '@/features/assets/search/asset-search.service';
import { notFoundError } from '@/utils/errors';

@Controller('/asset-edit')
export class AssetEditController {
Expand All @@ -41,17 +36,10 @@ export class AssetEditController {
async create(@ParseBody(PatchAsset) patch: PatchAsset, @CurrentUser() user: User) {
authorize(AssetEditPolicy, user).canCreate();
validatePatch(user, patch);
const result = await pipe(
TE.tryCatch(() => this.assetEditRepo.create({ user, patch }), unknownToUnknownError),
TE.chain(({ assetId }) => TE.tryCatch(() => this.assetEditRepo.find(assetId), unknownToUnknownError)),
TE.chainW(TE.fromPredicate(isNotNull, notFoundError)),
TE.tap((asset) => TE.tryCatch(() => this.assetSearchService.register(asset), unknownToUnknownError)),
TE.map((asset) => AssetEditDetail.encode(asset))
)();
if (E.isLeft(result)) {
throw new HttpException(result.left.message, 500);
}
return result.right;

const asset = await this.assetEditRepo.create({ user, patch });
await this.assetSearchService.register(asset);
return AssetEditDetail.encode(asset);
}

@Put('/:id')
Expand All @@ -68,16 +56,12 @@ export class AssetEditController {
authorize(AssetEditPolicy, user).canUpdate(record);
validatePatch(user, patch, record);

const result = await pipe(
TE.tryCatch(() => this.assetEditRepo.update(record.assetId, { user, patch }), unknownToUnknownError),
TE.chainW(TE.fromPredicate(isNotNull, notFoundError)),
TE.tap((asset) => TE.tryCatch(() => this.assetSearchService.register(asset), unknownToUnknownError)),
TE.map((asset) => AssetEditDetail.encode(asset))
)();
if (E.isLeft(result)) {
throw new HttpException(result.left.message, 500);
const asset = await this.assetEditRepo.update(record.assetId, { user, patch });
if (asset === null) {
throw new HttpException('not found', 404);
}
return result.right;
await this.assetSearchService.register(asset);
return AssetEditDetail.encode(asset);
}

@Delete('/:id')
Expand Down

0 comments on commit 6471094

Please sign in to comment.