|
1 | 1 | import { type Asset, AssetEntity } from "../entities/index.js";
|
2 | 2 | import { ReadAssetError } from "../errors/ReadAssetError.js";
|
3 | 3 | import { SaveAssetError } from "../errors/SaveAssetError.js";
|
4 |
| -import type { UpdateAssetInput } from "../gql-server/types.generated.js"; |
| 4 | +import type { |
| 5 | + CreateAssetInput, |
| 6 | + UpdateAssetInput, |
| 7 | +} from "../gql-server/types.generated.js"; |
5 | 8 | import { EntityManagerService } from "../services/index.js";
|
6 |
| -import { mutateImages } from "./image.js"; |
7 |
| -import { mutateProofOfPurchase } from "./proofOfPurchase.js"; |
| 9 | +import { createImage, mutateImages } from "./image.js"; |
| 10 | +import { |
| 11 | + mutateProofOfPurchase, |
| 12 | + updateProofOfPurchase, |
| 13 | +} from "./proofOfPurchase.js"; |
8 | 14 | import { Array, Effect, Option, pipe } from "effect";
|
9 | 15 | import type { FindOptionsRelations } from "typeorm";
|
10 | 16 |
|
@@ -36,22 +42,54 @@ export const readAsset = (
|
36 | 42 | });
|
37 | 43 | });
|
38 | 44 |
|
| 45 | +const saveAsset = (input: Partial<Asset>) => |
| 46 | + Effect.gen(function* () { |
| 47 | + const manager = yield* EntityManagerService; |
| 48 | + |
| 49 | + return yield* Effect.tryPromise({ |
| 50 | + try: async () => (await manager.save(AssetEntity, input)) as Asset, |
| 51 | + catch: (cause) => |
| 52 | + new SaveAssetError({ |
| 53 | + message: "Failed to save asset", |
| 54 | + options: { cause, input }, |
| 55 | + }), |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | +export const createAsset = (input: CreateAssetInput) => |
| 60 | + pipe( |
| 61 | + saveAsset({ |
| 62 | + name: input.name, |
| 63 | + description: input.description, |
| 64 | + }), |
| 65 | + Effect.andThen((asset) => |
| 66 | + Option.match(Option.fromNullable(input.proofOfPurchase), { |
| 67 | + onSome: (proofOfPurchase) => |
| 68 | + updateProofOfPurchase(asset, proofOfPurchase), |
| 69 | + onNone: () => Effect.succeed(asset), |
| 70 | + }), |
| 71 | + ), |
| 72 | + Effect.andThen((asset) => |
| 73 | + Option.match( |
| 74 | + Option.filter(Option.fromNullable(input.images), Array.isNonEmptyArray), |
| 75 | + { |
| 76 | + onSome: (images) => |
| 77 | + Effect.reduce(Array.reverse(images), asset, (asset, image) => |
| 78 | + createImage(asset, image), |
| 79 | + ), |
| 80 | + onNone: () => Effect.succeed(asset), |
| 81 | + }, |
| 82 | + ), |
| 83 | + ), |
| 84 | + ); |
| 85 | + |
39 | 86 | export const updateAsset = (asset: Asset, input: UpdateAssetInput) =>
|
40 | 87 | pipe(
|
41 | 88 | Effect.gen(function* () {
|
42 |
| - const manager = yield* EntityManagerService; |
43 |
| - |
44 | 89 | if (input.name != null) asset.name = input.name;
|
45 | 90 | if (input.description != null) asset.description = input.description;
|
46 | 91 |
|
47 |
| - return yield* Effect.tryPromise({ |
48 |
| - try: async () => await manager.save(AssetEntity, asset), |
49 |
| - catch: (cause) => |
50 |
| - new SaveAssetError({ |
51 |
| - message: "Failed to save document", |
52 |
| - options: { cause, input }, |
53 |
| - }), |
54 |
| - }); |
| 92 | + return yield* saveAsset(asset); |
55 | 93 | }),
|
56 | 94 | Effect.andThen((asset) =>
|
57 | 95 | Option.match(Option.fromNullable(input.proofOfPurchase), {
|
|
0 commit comments