Skip to content

Commit

Permalink
Sort pubs before limiting (#923)
Browse files Browse the repository at this point in the history
* Sort pubs before limiting

* Update pub's updatedAt when moving it to a new stage

* Consolidate pub moving functions

* Use passed transaction in updatePub

* Suppress error when moving pub into the stage it's already in

* Add sort test

* Make pub db tests clean up after themselves

* Use testDb like in pub-trigger tests

---------

Co-authored-by: Eric McDaniel <eric.g.mcdaniel@gmail.com>
  • Loading branch information
kalilsn and 3mcd authored Feb 10, 2025
1 parent 1dca72e commit cb45900
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 49 deletions.
22 changes: 4 additions & 18 deletions core/actions/move/run.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
"use server";

import type { PubsId, StagesId } from "db/public";
import type { StagesId } from "db/public";
import { logger } from "logger";

import type { action } from "./action";
import { db } from "~/kysely/database";
import { autoRevalidate } from "~/lib/server/cache/autoRevalidate";
import { movePub } from "~/lib/server/stages";
import { defineRun } from "../types";

export const run = defineRun<typeof action>(async ({ pub, config, stageId }) => {
export const run = defineRun<typeof action>(async ({ pub, config }) => {
try {
await autoRevalidate(
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();
await movePub(pub.id, config.stage as StagesId).execute();
} catch (error) {
logger.error({ msg: "move", error });
return {
Expand Down
13 changes: 2 additions & 11 deletions core/app/c/[communitySlug]/stages/components/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { userCan } from "~/lib/authorization/capabilities";
import { ApiError } from "~/lib/server";
import { autoRevalidate } from "~/lib/server/cache/autoRevalidate";
import { defineServerAction } from "~/lib/server/defineServerAction";
import { movePub } from "~/lib/server/stages";

export const move = defineServerAction(async function move(
pubId: PubsId,
Expand All @@ -33,17 +34,7 @@ export const move = defineServerAction(async function move(
}

try {
await autoRevalidate(
db
.with("removed_pubsInStages", (db) =>
db
.deleteFrom("PubsInStages")
.where("pubId", "=", pubId)
.where("stageId", "=", sourceStageId)
)
.insertInto("PubsInStages")
.values([{ pubId: pubId, stageId: destinationStageId }])
).executeTakeFirstOrThrow();
await movePub(pubId, destinationStageId).executeTakeFirstOrThrow();
} catch {
return { error: "The Pub was not successully moved" };
}
Expand Down
Loading

0 comments on commit cb45900

Please sign in to comment.