Skip to content

Commit c7f451b

Browse files
committed
simplify route reads
1 parent d789912 commit c7f451b

3 files changed

Lines changed: 11 additions & 31 deletions

File tree

apps/webapp/app/presenters/v3/ApiBulkActionPresenter.server.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,6 @@ type BulkActionRow = Pick<
3737
>;
3838

3939
export class ApiBulkActionPresenter extends BasePresenter {
40-
public async retrieve(environmentId: string, bulkActionId: string) {
41-
// Read from primary so create -> retrieve/poll doesn't 404 on replica lag.
42-
const bulkAction = await this._prisma.bulkActionGroup.findFirst({
43-
select: bulkActionSelect,
44-
where: {
45-
environmentId,
46-
friendlyId: bulkActionId,
47-
},
48-
});
49-
50-
if (!bulkAction) {
51-
return undefined;
52-
}
53-
54-
return apiBulkActionObject(bulkAction);
55-
}
56-
5740
public async list(environmentId: string, searchParams: ApiBulkActionListSearchParams) {
5841
const pageSize = searchParams["page[size]"] ?? DEFAULT_PAGE_SIZE;
5942
const after = searchParams["page[after]"];
@@ -116,7 +99,7 @@ export class ApiBulkActionPresenter extends BasePresenter {
11699
}
117100
}
118101

119-
const bulkActionSelect = {
102+
export const bulkActionSelect = {
120103
id: true,
121104
friendlyId: true,
122105
name: true,

apps/webapp/app/routes/api.v1.bulk-actions.$bulkActionId.abort.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const { action } = createActionApiRoute(
1818
action: "write",
1919
resource: () => ({ type: "runs" }),
2020
},
21+
// Existence/auth gate. Reads from primary so create -> abort doesn't 404 on
22+
// replica lag; the abort write path re-reads and mutates on primary.
2123
findResource: async (params, auth) => {
22-
// Read from primary so create -> abort doesn't 404 on replica lag.
2324
return prisma.bulkActionGroup.findFirst({
2425
select: { id: true },
2526
where: {
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { json } from "@remix-run/server-runtime";
22
import { z } from "zod";
33
import { prisma } from "~/db.server";
4-
import { ApiBulkActionPresenter } from "~/presenters/v3/ApiBulkActionPresenter.server";
4+
import {
5+
apiBulkActionObject,
6+
bulkActionSelect,
7+
} from "~/presenters/v3/ApiBulkActionPresenter.server";
58
import { createLoaderApiRoute } from "~/services/routeBuilders/apiBuilder.server";
69

710
const ParamsSchema = z.object({
@@ -16,25 +19,18 @@ export const loader = createLoaderApiRoute(
1619
action: "read",
1720
resource: () => ({ type: "runs" }),
1821
},
22+
// Read from primary so create -> retrieve/poll doesn't 404 on replica lag.
1923
findResource: async (params, auth) => {
20-
// Read from primary so create -> retrieve/poll doesn't 404 on replica lag.
2124
return prisma.bulkActionGroup.findFirst({
22-
select: { id: true },
25+
select: bulkActionSelect,
2326
where: {
2427
friendlyId: params.bulkActionId,
2528
environmentId: auth.environment.id,
2629
},
2730
});
2831
},
2932
},
30-
async ({ params, authentication }) => {
31-
const presenter = new ApiBulkActionPresenter();
32-
const bulkAction = await presenter.retrieve(authentication.environment.id, params.bulkActionId);
33-
34-
if (!bulkAction) {
35-
return json({ error: "Bulk action not found" }, { status: 404 });
36-
}
37-
38-
return json(bulkAction);
33+
async ({ resource }) => {
34+
return json(apiBulkActionObject(resource));
3935
}
4036
);

0 commit comments

Comments
 (0)