-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 47 additions & 45 deletions
92
apps/server-asset-sg/src/features/asset-edit/asset-edit.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,55 @@ | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import * as O from 'fp-ts/Option'; | ||
|
||
import { isNotNull, unknownToUnknownError } from '@asset-sg/core'; | ||
import { BaseAssetEditDetail, PatchAsset } from '@asset-sg/shared'; | ||
import { User } from '@asset-sg/shared/v2'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { pipe } from 'fp-ts/function'; | ||
import * as TE from 'fp-ts/TaskEither'; | ||
import * as C from 'io-ts/Codec'; | ||
|
||
import { AssetEditRepo } from './asset-edit.repo'; | ||
import { AssetSearchService } from '@/features/assets/search/asset-search.service'; | ||
import { notFoundError } from '@/utils/errors'; | ||
|
||
export const AssetEditDetail = C.struct({ | ||
...BaseAssetEditDetail, | ||
studies: C.array(C.struct({ assetId: C.number, studyId: C.string, geomText: C.string })), | ||
}); | ||
export type AssetEditDetail = C.TypeOf<typeof AssetEditDetail>; | ||
import { AssetEditData } from './asset-edit.repo'; | ||
import { PrismaService } from '@/core/prisma.service'; | ||
|
||
@Injectable() | ||
export class AssetEditService { | ||
constructor(private readonly assetEditRepo: AssetEditRepo, private readonly assetSearchService: AssetSearchService) {} | ||
constructor(private readonly prismaService: PrismaService) {} | ||
|
||
public createAsset(user: User, patch: PatchAsset) { | ||
return pipe( | ||
TE.tryCatch( | ||
() => this.assetEditRepo.create({ user, patch }), | ||
(e) => e as Error | ||
), | ||
TE.chain(({ assetId }) => | ||
TE.tryCatch( | ||
() => this.assetEditRepo.find(assetId), | ||
(e) => e as Error | ||
) | ||
), | ||
TE.chainW(TE.fromPredicate(isNotNull, notFoundError)), | ||
TE.tap((asset) => TE.tryCatch(() => this.assetSearchService.register(asset), unknownToUnknownError)), | ||
TE.map((asset) => AssetEditDetail.encode(asset)) | ||
); | ||
} | ||
public async validateReferencesOrThrow(data: AssetEditData, id?: number): Promise<void> { | ||
// check if any of the siblings are in another workgroup | ||
for (const assetYId of data.patch.siblingAssetIds) { | ||
const siblingCandidate = await this.prismaService.asset.findUnique({ | ||
where: { assetId: assetYId }, | ||
select: { workgroupId: true }, | ||
}); | ||
if (siblingCandidate?.workgroupId !== data.patch.workgroupId) { | ||
throw new HttpException( | ||
'Sibling assets must be in the same workgroup as the edited asset', | ||
HttpStatus.UNPROCESSABLE_ENTITY | ||
); | ||
} | ||
} | ||
|
||
// check if the parent asset is in another workgroup | ||
const assetMainId = O.toUndefined(data.patch.assetMainId); | ||
if (assetMainId) { | ||
const assetMain = await this.prismaService.asset.findUnique({ | ||
where: { assetId: assetMainId }, | ||
select: { workgroupId: true }, | ||
}); | ||
if (assetMain?.workgroupId !== data.patch.workgroupId) { | ||
throw new HttpException('Cannot assign parent asset from different workgroup', HttpStatus.UNPROCESSABLE_ENTITY); | ||
} | ||
} | ||
|
||
// check if any of the subordinate assets are in another workgroup for exisiting assets | ||
if (id) { | ||
const childAssets = await this.prismaService.asset.findMany({ | ||
where: { assetMainId: id }, | ||
select: { workgroupId: true }, | ||
}); | ||
|
||
public updateAsset(user: User, assetId: number, patch: PatchAsset) { | ||
return pipe( | ||
TE.tryCatch( | ||
() => this.assetEditRepo.update(assetId, { user, patch }), | ||
(e) => e as Error | ||
), | ||
TE.chainW(TE.fromPredicate(isNotNull, notFoundError)), | ||
TE.tap((asset) => TE.tryCatch(() => this.assetSearchService.register(asset), unknownToUnknownError)), | ||
TE.map((asset) => AssetEditDetail.encode(asset)) | ||
); | ||
for (const child of childAssets) { | ||
if (child.workgroupId !== data.patch.workgroupId) { | ||
throw new HttpException( | ||
'Child assets must be in the same workgroup as the parent asset', | ||
HttpStatus.UNPROCESSABLE_ENTITY | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |