Repository navigation
Expand file tree
/
Copy pathPullRequestFilesViewed.ts
More file actions
151 lines (137 loc) · 5.03 KB
/
Copy pathPullRequestFilesViewed.ts
File metadata and controls
151 lines (137 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import * as SqlClient from "effect/sql/SqlClient";
import * as SqlSchema from "effect/sql/SqlSchema";
import { SourceControlProviderKind } from "@t3tools/contracts";
import {
PersistenceDecodeError,
PersistenceSqlError,
type PullRequestFilesViewedRepositoryError,
} from "./Errors.ts";
export const PullRequestFilesViewedScope = Schema.Struct({
provider: SourceControlProviderKind,
host: Schema.String,
repository: Schema.String,
number: Schema.Int,
viewer: Schema.String,
});
export type PullRequestFilesViewedScope = typeof PullRequestFilesViewedScope.Type;
export const PullRequestFileViewedMark = Schema.Struct({
path: Schema.String,
/**
* Empty where the host said it had none to give, which is an answer rather than a gap: a file
* with no version at the head is one the change request deletes. Null where the host could
* not say at all, which is no baseline rather than an empty one, and such a mark stays
* cleared until a press replaces it.
*/
revision: Schema.NullOr(Schema.String),
});
export type PullRequestFileViewedMark = typeof PullRequestFileViewedMark.Type;
export interface SetPullRequestFilesViewedInput extends PullRequestFilesViewedScope {
readonly files: ReadonlyArray<PullRequestFileViewedMark & { readonly viewed: boolean }>;
readonly viewedAt: string;
}
export const MAX_FILES_VIEWED_ROWS = 500;
export interface PullRequestFilesViewedPage {
readonly files: ReadonlyArray<PullRequestFileViewedMark>;
readonly truncated: boolean;
}
export class PullRequestFilesViewedRepository extends Context.Service<
PullRequestFilesViewedRepository,
{
readonly list: (
input: PullRequestFilesViewedScope,
) => Effect.Effect<PullRequestFilesViewedPage, PullRequestFilesViewedRepositoryError>;
readonly set: (
input: SetPullRequestFilesViewedInput,
) => Effect.Effect<void, PullRequestFilesViewedRepositoryError>;
}
>()("t3/persistence/PullRequestFilesViewed/PullRequestFilesViewedRepository") {}
function toSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
return (cause: unknown): PullRequestFilesViewedRepositoryError =>
Schema.isSchemaError(cause)
? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
: new PersistenceSqlError({ operation: sqlOperation, cause });
}
const make = Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
const listRows = SqlSchema.findAll({
Request: PullRequestFilesViewedScope,
Result: PullRequestFileViewedMark,
execute: ({ provider, host, repository, number, viewer }) =>
sql`
SELECT
path AS "path",
revision AS "revision"
FROM pull_request_files_viewed
WHERE provider = ${provider}
AND host = ${host}
AND repository = ${repository}
AND number = ${number}
AND viewer = ${viewer}
ORDER BY path
LIMIT ${MAX_FILES_VIEWED_ROWS + 1}
`,
});
return PullRequestFilesViewedRepository.of({
list: (input) =>
listRows(input).pipe(
Effect.map((rows) => ({
files: rows.slice(0, MAX_FILES_VIEWED_ROWS),
truncated: rows.length > MAX_FILES_VIEWED_ROWS,
})),
Effect.mapError(toSqlOrDecodeError("listPullRequestFilesViewed", "PullRequestFileViewed")),
),
set: (input) =>
sql
.withTransaction(
Effect.forEach(
input.files,
(file) =>
file.viewed
? sql`
INSERT INTO pull_request_files_viewed (
provider,
host,
repository,
number,
viewer,
path,
revision,
viewed_at
)
VALUES (
${input.provider},
${input.host},
${input.repository},
${input.number},
${input.viewer},
${file.path},
${file.revision},
${input.viewedAt}
)
ON CONFLICT (provider, host, repository, number, viewer, path)
DO UPDATE SET revision = excluded.revision, viewed_at = excluded.viewed_at
`
: sql`
DELETE FROM pull_request_files_viewed
WHERE provider = ${input.provider}
AND host = ${input.host}
AND repository = ${input.repository}
AND number = ${input.number}
AND viewer = ${input.viewer}
AND path = ${file.path}
`,
{ discard: true },
),
)
.pipe(
Effect.mapError(
(cause) => new PersistenceSqlError({ operation: "setPullRequestFilesViewed", cause }),
),
),
});
});
export const layer = Layer.effect(PullRequestFilesViewedRepository, make);