Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectionPendingApprovals.ts

175 lines5.5 KB

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";
5import * as Schema from "effect/Schema";
6​
7import { toPersistenceSqlError } from "../Errors.ts";
8import {
9 GetProjectionPendingApprovalInput,
10 DeleteProjectionPendingApprovalInput,
11 ListProjectionPendingApprovalsInput,
12 ProjectionPendingApproval,
13 ProjectionPendingApprovalRepository,
14 type ProjectionPendingApprovalRepositoryShape,
15} from "../Services/ProjectionPendingApprovals.ts";
16​
17const makeProjectionPendingApprovalRepository = Effect.gen(function* () {
18 const sql = yield* SqlClient.SqlClient;
19​
20 const upsertProjectionPendingApprovalRow = SqlSchema.void({
21 Request: ProjectionPendingApproval,
22 execute: (row) =>
23 sql`
24 INSERT INTO projection_pending_approvals (
25 request_id,
26 thread_id,
27 turn_id,
28 status,
29 decision,
30 created_at,
31 resolved_at
32 )
33 VALUES (
34 ${row.requestId},
35 ${row.threadId},
36 ${row.turnId},
37 ${row.status},
38 ${row.decision},
39 ${row.createdAt},
40 ${row.resolvedAt}
41 )
42 ON CONFLICT (request_id)
43 DO UPDATE SET
44 thread_id = excluded.thread_id,
45 turn_id = excluded.turn_id,
46 status = excluded.status,
47 decision = excluded.decision,
48 created_at = excluded.created_at,
49 resolved_at = excluded.resolved_at
50 `,
51 });
52​
53 const listProjectionPendingApprovalRows = SqlSchema.findAll({
54 Request: ListProjectionPendingApprovalsInput,
55 Result: ProjectionPendingApproval,
56 execute: ({ threadId }) =>
57 sql`
58 SELECT
59 request_id AS "requestId",
60 thread_id AS "threadId",
61 turn_id AS "turnId",
62 status,
63 decision,
64 created_at AS "createdAt",
65 resolved_at AS "resolvedAt"
66 FROM projection_pending_approvals
67 WHERE thread_id = ${threadId}
68 ORDER BY created_at ASC, request_id ASC
69 `,
70 });
71​
72 const countPendingApprovalRows = SqlSchema.findOne({
73 Request: ListProjectionPendingApprovalsInput,
74 Result: Schema.Struct({ count: Schema.Number }),
75 execute: ({ threadId }) => sql`
76 SELECT COUNT(*) AS count
77 FROM projection_pending_approvals
78 WHERE thread_id = ${threadId} AND status = 'pending'
79 `,
80 });
81​
82 const getProjectionPendingApprovalRow = SqlSchema.findOneOption({
83 Request: GetProjectionPendingApprovalInput,
84 Result: ProjectionPendingApproval,
85 execute: ({ requestId }) =>
86 sql`
87 SELECT
88 request_id AS "requestId",
89 thread_id AS "threadId",
90 turn_id AS "turnId",
91 status,
92 decision,
93 created_at AS "createdAt",
94 resolved_at AS "resolvedAt"
95 FROM projection_pending_approvals
96 WHERE request_id = ${requestId}
97 `,
98 });
99​
100 const deleteProjectionPendingApprovalRow = SqlSchema.void({
101 Request: DeleteProjectionPendingApprovalInput,
102 execute: ({ requestId }) =>
103 sql`
104 DELETE FROM projection_pending_approvals
105 WHERE request_id = ${requestId}
106 `,
107 });
108​
109 const deleteProjectionPendingApprovalRowsByThread = SqlSchema.void({
110 Request: ListProjectionPendingApprovalsInput,
111 execute: ({ threadId }) =>
112 sql`
113 DELETE FROM projection_pending_approvals
114 WHERE thread_id = ${threadId}
115 `,
116 });
117​
118 const upsert: ProjectionPendingApprovalRepositoryShape["upsert"] = (row) =>
119 upsertProjectionPendingApprovalRow(row).pipe(
120 Effect.mapError(toPersistenceSqlError("ProjectionPendingApprovalRepository.upsert:query")),
121 );
122​
123 const listByThreadId: ProjectionPendingApprovalRepositoryShape["listByThreadId"] = (input) =>
124 listProjectionPendingApprovalRows(input).pipe(
125 Effect.mapError(
126 toPersistenceSqlError("ProjectionPendingApprovalRepository.listByThreadId:query"),
127 ),
128 );
129​
130 const countPendingByThreadId: ProjectionPendingApprovalRepositoryShape["countPendingByThreadId"] =
131 (input) =>
132 countPendingApprovalRows(input).pipe(
133 Effect.mapError(
134 toPersistenceSqlError("ProjectionPendingApprovalRepository.countPendingByThreadId:query"),
135 ),
136 Effect.map((row) => row.count),
137 );
138​
139 const getByRequestId: ProjectionPendingApprovalRepositoryShape["getByRequestId"] = (input) =>
140 getProjectionPendingApprovalRow(input).pipe(
141 Effect.mapError(
142 toPersistenceSqlError("ProjectionPendingApprovalRepository.getByRequestId:query"),
143 ),
144 );
145​
146 const deleteByRequestId: ProjectionPendingApprovalRepositoryShape["deleteByRequestId"] = (
147 input,
148 ) =>
149 deleteProjectionPendingApprovalRow(input).pipe(
150 Effect.mapError(
151 toPersistenceSqlError("ProjectionPendingApprovalRepository.deleteByRequestId:query"),
152 ),
153 );
154​
155 const deleteByThreadId: ProjectionPendingApprovalRepositoryShape["deleteByThreadId"] = (input) =>
156 deleteProjectionPendingApprovalRowsByThread(input).pipe(
157 Effect.mapError(
158 toPersistenceSqlError("ProjectionPendingApprovalRepository.deleteByThreadId:query"),
159 ),
160 );
161​
162 return {
163 upsert,
164 listByThreadId,
165 countPendingByThreadId,
166 getByRequestId,
167 deleteByRequestId,
168 deleteByThreadId,
169 } satisfies ProjectionPendingApprovalRepositoryShape;
170});
171​
172export const ProjectionPendingApprovalRepositoryLive = Layer.effect(
173 ProjectionPendingApprovalRepository,
174 makeProjectionPendingApprovalRepository,
175);