diff --git a/core/actions/_lib/runActionInstance.ts b/core/actions/_lib/runActionInstance.ts index af0ffd3b4..60011e814 100644 --- a/core/actions/_lib/runActionInstance.ts +++ b/core/actions/_lib/runActionInstance.ts @@ -1,5 +1,6 @@ "use server"; +import { revalidateTag } from "next/cache"; import { captureException } from "@sentry/nextjs"; import { sql } from "kysely"; @@ -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); diff --git a/core/actions/api/index.ts b/core/actions/api/index.ts index de4868c3b..933e0b90c 100644 --- a/core/actions/api/index.ts +++ b/core/actions/api/index.ts @@ -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"; @@ -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) => { diff --git a/core/actions/move/action.ts b/core/actions/move/action.ts new file mode 100644 index 000000000..f74557028 --- /dev/null +++ b/core/actions/move/action.ts @@ -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, +}); diff --git a/core/actions/move/run.ts b/core/actions/move/run.ts new file mode 100644 index 000000000..e92be1ddd --- /dev/null +++ b/core/actions/move/run.ts @@ -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(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: {}, + }; +}); diff --git a/core/actions/runs.ts b/core/actions/runs.ts index 8080467e5..44dae2ff6 100644 --- a/core/actions/runs.ts +++ b/core/actions/runs.ts @@ -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"; diff --git a/core/actions/types.ts b/core/actions/types.ts index 8fa58601d..8de6c2a77 100644 --- a/core/actions/types.ts +++ b/core/actions/types.ts @@ -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[]; @@ -17,7 +18,7 @@ export type ActionPub = { export type RunProps = T extends Action - ? { config: AC; pub: ActionPub; runParameters: RP } + ? { config: AC; pub: ActionPub; runParameters: RP; stageId: StagesId } : never; export type ConfigProps = { diff --git a/core/kysely/types/public/Action.ts b/core/kysely/types/public/Action.ts index 3db41fde0..502e771ce 100644 --- a/core/kysely/types/public/Action.ts +++ b/core/kysely/types/public/Action.ts @@ -7,6 +7,7 @@ enum Action { pdf = "pdf", email = "email", pushToV6 = "pushToV6", + move = "move", } export default Action; diff --git a/core/kysely/types/public/PublicSchema.ts b/core/kysely/types/public/PublicSchema.ts index bc3513e3f..ca7195af0 100644 --- a/core/kysely/types/public/PublicSchema.ts +++ b/core/kysely/types/public/PublicSchema.ts @@ -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; @@ -83,4 +81,6 @@ export default interface PublicSchema { action_instances: ActionInstancesTable; PubsInStages: PubsInStagesTable; + + rules: RulesTable; } diff --git a/core/prisma/migrations/20240506184830_add_move_action/migration.sql b/core/prisma/migrations/20240506184830_add_move_action/migration.sql new file mode 100644 index 000000000..1c950ff64 --- /dev/null +++ b/core/prisma/migrations/20240506184830_add_move_action/migration.sql @@ -0,0 +1,2 @@ +-- AlterEnum +ALTER TYPE "Action" ADD VALUE 'move'; diff --git a/core/prisma/schema.prisma b/core/prisma/schema.prisma index 24fb6dccb..03a2b0417 100644 --- a/core/prisma/schema.prisma +++ b/core/prisma/schema.prisma @@ -347,6 +347,7 @@ enum Action { pdf email pushToV6 + move } model Rule { diff --git a/packages/contracts/src/resources/integrations.ts b/packages/contracts/src/resources/integrations.ts index 38dced6cc..ae63b8b75 100644 --- a/packages/contracts/src/resources/integrations.ts +++ b/packages/contracts/src/resources/integrations.ts @@ -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; diff --git a/packages/ui/src/icon.tsx b/packages/ui/src/icon.tsx index 27ee27a75..f7ed43cca 100644 --- a/packages/ui/src/icon.tsx +++ b/packages/ui/src/icon.tsx @@ -17,6 +17,7 @@ export { Mail, Menu, MoreVertical, + MoveHorizontal, Pencil, Plus, Play,