Open History to see the last commit
1import * as SqlClient from "effect/unstable/sql/SqlClient";
2import * as SqlSchema from "effect/unstable/sql/SqlSchema";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5
6import { toPersistenceSqlError } from "../Errors.ts";
7import {
8 GetProjectionPendingApprovalInput,
9 DeleteProjectionPendingApprovalInput,
10 ListProjectionPendingApprovalsInput,
11 ProjectionPendingApproval,
12 ProjectionPendingApprovalRepository,
13 type ProjectionPendingApprovalRepositoryShape,
14} from "../Services/ProjectionPendingApprovals.ts";
15
16const makeProjectionPendingApprovalRepository = Effect.gen(function* () {
17 const sql = yield* SqlClient.SqlClient;
18
19 const upsertProjectionPendingApprovalRow = SqlSchema.void({
20 Request: ProjectionPendingApproval,
21 execute: (row) =>
22 sql`
23 INSERT INTO projection_pending_approvals (
24 request_id,
25 thread_id,
26 turn_id,
27 status,
28 decision,
29 created_at,
30 resolved_at
31 )
32 VALUES (
33 ${row.requestId},
34 ${row.threadId},
35 ${row.turnId},
36 ${row.status},
37 ${row.decision},
38 ${row.createdAt},
39 ${row.resolvedAt}
40 )
41 ON CONFLICT (request_id)
42 DO UPDATE SET
43 thread_id = excluded.thread_id,
44 turn_id = excluded.turn_id,
45 status = excluded.status,
46 decision = excluded.decision,
47 created_at = excluded.created_at,
48 resolved_at = excluded.resolved_at
49 `,
50 });
51
52 const listProjectionPendingApprovalRows = SqlSchema.findAll({
53 Request: ListProjectionPendingApprovalsInput,
54 Result: ProjectionPendingApproval,
55 execute: ({ threadId }) =>
56 sql`
57 SELECT
58 request_id AS "requestId",
59 thread_id AS "threadId",
60 turn_id AS "turnId",
61 status,
62 decision,
63 created_at AS "createdAt",
64 resolved_at AS "resolvedAt"
65 FROM projection_pending_approvals
66 WHERE thread_id = ${threadId}
67 ORDER BY created_at ASC, request_id ASC
68 `,
69 });
70
71 const getProjectionPendingApprovalRow = SqlSchema.findOneOption({
72 Request: GetProjectionPendingApprovalInput,
73 Result: ProjectionPendingApproval,
74 execute: ({ requestId }) =>
75 sql`
76 SELECT
77 request_id AS "requestId",
78 thread_id AS "threadId",
79 turn_id AS "turnId",
80 status,
81 decision,
82 created_at AS "createdAt",
83 resolved_at AS "resolvedAt"
84 FROM projection_pending_approvals
85 WHERE request_id = ${requestId}
86 `,
87 });
88
89 const deleteProjectionPendingApprovalRow = SqlSchema.void({
90 Request: DeleteProjectionPendingApprovalInput,
91 execute: ({ requestId }) =>
92 sql`
93 DELETE FROM projection_pending_approvals
94 WHERE request_id = ${requestId}
95 `,
96 });
97
98 const upsert: ProjectionPendingApprovalRepositoryShape["upsert"] = (row) =>
99 upsertProjectionPendingApprovalRow(row).pipe(
100 Effect.mapError(toPersistenceSqlError("ProjectionPendingApprovalRepository.upsert:query")),
101 );
102
103 const listByThreadId: ProjectionPendingApprovalRepositoryShape["listByThreadId"] = (input) =>
104 listProjectionPendingApprovalRows(input).pipe(
105 Effect.mapError(
106 toPersistenceSqlError("ProjectionPendingApprovalRepository.listByThreadId:query"),
107 ),
108 );
109
110 const getByRequestId: ProjectionPendingApprovalRepositoryShape["getByRequestId"] = (input) =>
111 getProjectionPendingApprovalRow(input).pipe(
112 Effect.mapError(
113 toPersistenceSqlError("ProjectionPendingApprovalRepository.getByRequestId:query"),
114 ),
115 );
116
117 const deleteByRequestId: ProjectionPendingApprovalRepositoryShape["deleteByRequestId"] = (
118 input,
119 ) =>
120 deleteProjectionPendingApprovalRow(input).pipe(
121 Effect.mapError(
122 toPersistenceSqlError("ProjectionPendingApprovalRepository.deleteByRequestId:query"),
123 ),
124 );
125
126 return {
127 upsert,
128 listByThreadId,
129 getByRequestId,
130 deleteByRequestId,
131 } satisfies ProjectionPendingApprovalRepositoryShape;
132});
133
134export const ProjectionPendingApprovalRepositoryLive = Layer.effect(
135 ProjectionPendingApprovalRepository,
136 makeProjectionPendingApprovalRepository,
137);