Skip to content

Commit

Permalink
Add move action (#340)
Browse files Browse the repository at this point in the history
* Add move action

* Use transaction

* Use cte instead of transaction

---------

Co-authored-by: Kalil Smith-Nuevelle <[email protected]>
  • Loading branch information
3mcd and kalilsn authored May 7, 2024
1 parent e915d30 commit fca322c
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 3 deletions.
4 changes: 4 additions & 0 deletions core/actions/_lib/runActionInstance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use server";

import { revalidateTag } from "next/cache";
import { captureException } from "@sentry/nextjs";
import { sql } from "kysely";

Expand Down Expand Up @@ -117,8 +118,11 @@ const _runActionInstance = async ({
values: values as any,
},
runParameters: runParameters,
stageId: actionInstance.stageId,
});

revalidateTag(`community-stages_${pub.communityId}`);

return result;
} catch (error) {
captureException(error);
Expand Down
2 changes: 2 additions & 0 deletions core/actions/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type Event from "~/kysely/types/public/Event";
import * as email from "../email/action";
import * as log from "../log/action";
import * as move from "../move/action";
import * as pdf from "../pdf/action";
import * as pushToV6 from "../pushToV6/action";

Expand All @@ -11,6 +12,7 @@ export const actions = {
[pdf.action.name]: pdf.action,
[email.action.name]: email.action,
[pushToV6.action.name]: pushToV6.action,
[move.action.name]: move.action,
} as const;

export const getActionByName = (name: keyof typeof actions) => {
Expand Down
16 changes: 16 additions & 0 deletions core/actions/move/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as z from "zod";

import { MoveHorizontal } from "ui/icon";

import { defineAction } from "../types";

export const action = defineAction({
name: "move",
config: z.object({
stage: z.string().describe("Destination stage"),
}),
description: "Move a pub to a different stage",
runParameters: z.object({}).optional(),
pubFields: [],
icon: MoveHorizontal,
});
42 changes: 42 additions & 0 deletions core/actions/move/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use server";

import { logger } from "logger";

import type { action } from "./action";
import type { PubsId } from "~/kysely/types/public/Pubs";
import type { StagesId } from "~/kysely/types/public/Stages";
import { db } from "~/kysely/database";
import { defineRun } from "../types";

export const run = defineRun<typeof action>(async ({ pub, config, stageId }) => {
try {
await db
.with("leave-stage", (db) =>
db
.deleteFrom("PubsInStages")
.where("pubId", "=", pub.id as PubsId)
.where("stageId", "=", stageId)
)
.insertInto("PubsInStages")
.values({
pubId: pub.id as PubsId,
stageId: config.stage as StagesId,
})
.execute();
} catch (error) {
logger.error({ msg: "move", error });
return {
title: "Failed to move pub",
error: "An error occured while moving the pub",
cause: error,
};
}

logger.info({ msg: "move", pub, config });

return {
success: true,
report: "Pub moved",
data: {},
};
});
1 change: 1 addition & 0 deletions core/actions/runs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { run as pdf } from "./pdf/run";
export { run as email } from "./email/run";
export { run as log } from "./log/run";
export { run as move } from "./move/run";
export { run as pushToV6 } from "./pushToV6/run";
3 changes: 2 additions & 1 deletion core/actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type * as Icons from "ui/icon";

import type { CorePubField } from "./corePubFields";
import type { ClientExceptionOptions } from "~/lib/serverActions";
import { StagesId } from "~/kysely/types/public/Stages";

export type ActionPubType = CorePubField[];

Expand All @@ -17,7 +18,7 @@ export type ActionPub<T extends ActionPubType> = {

export type RunProps<T extends Action> =
T extends Action<infer PT, infer AC, infer RP>
? { config: AC; pub: ActionPub<PT>; runParameters: RP }
? { config: AC; pub: ActionPub<PT>; runParameters: RP; stageId: StagesId }
: never;

export type ConfigProps<C> = {
Expand Down
1 change: 1 addition & 0 deletions core/kysely/types/public/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum Action {
pdf = "pdf",
email = "email",
pushToV6 = "pushToV6",
move = "move",
}

export default Action;
4 changes: 2 additions & 2 deletions core/kysely/types/public/PublicSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import { type default as StagesTable } from "./Stages";
import { type default as UsersTable } from "./Users";

export default interface PublicSchema {
rules: RulesTable;

_prisma_migrations: PrismaMigrationsTable;

users: UsersTable;
Expand Down Expand Up @@ -83,4 +81,6 @@ export default interface PublicSchema {
action_instances: ActionInstancesTable;

PubsInStages: PubsInStagesTable;

rules: RulesTable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterEnum
ALTER TYPE "Action" ADD VALUE 'move';
1 change: 1 addition & 0 deletions core/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ enum Action {
pdf
email
pushToV6
move
}

model Rule {
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/resources/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const GetPubResponseBodyBase = commonPubFields.extend({
id: z.string(),
values: z.record(JsonOutput),
assignee: User.optional(),
communityId: z.string(),
});
export type GetPubResponseBodyBase = z.infer<typeof GetPubResponseBodyBase>;

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
Mail,
Menu,
MoreVertical,
MoveHorizontal,
Pencil,
Plus,
Play,
Expand Down

0 comments on commit fca322c

Please sign in to comment.