Implement update_qa_entry_with_publish reducer to streamline QA entry updates with publish state

This commit is contained in:
Aadesh Kheria 2026-05-05 13:10:16 -07:00
parent 5552bd7104
commit 2e78347464
5 changed files with 62 additions and 2 deletions

View File

@ -34,8 +34,14 @@ export const POST = createSmartRouteHandler({
const token = getEnvVariable("STACK_MCP_LOG_TOKEN");
const editor = user.display_name ?? user.primary_email ?? user.id;
const qaId = BigInt(body.qaId);
await callReducerStrict("update_qa_entry", [token, qaId, body.question, body.answer, editor]);
await callReducerStrict("set_qa_published", [token, qaId, body.publish]);
await callReducerStrict("update_qa_entry_with_publish", [
token,
qaId,
body.question,
body.answer,
body.publish,
editor,
]);
return {
statusCode: 200,

View File

@ -491,6 +491,36 @@ export const set_qa_published = spacetimedb.reducer(
}
);
export const update_qa_entry_with_publish = spacetimedb.reducer(
{
token: t.string(),
qaId: t.u64(),
question: t.string(),
answer: t.string(),
publish: t.bool(),
editedBy: t.string(),
},
(ctx, args) => {
if (args.token !== EXPECTED_LOG_TOKEN) {
throw new SenderError('Invalid log token');
}
const row = ctx.db.qaEntries.id.find(args.qaId);
if (row == null) {
throw new SenderError('QA entry not found for qaId: ' + args.qaId.toString());
}
ctx.db.qaEntries.id.update({
...row,
question: args.question,
answer: args.answer,
lastEditedBy: args.editedBy,
lastEditedAt: ctx.timestamp,
published: args.publish,
firstPublishedAt: args.publish ? (row.firstPublishedAt ?? ctx.timestamp) : row.firstPublishedAt,
lastPublishedAt: args.publish ? ctx.timestamp : row.lastPublishedAt,
});
}
);
// Text-only edit. Doesn't touch publish state.
export const update_qa_entry = spacetimedb.reducer(
{

View File

@ -50,6 +50,7 @@ import UnmarkHumanReviewedReducer from "./unmark_human_reviewed_reducer";
import UpdateAiQueryCostReducer from "./update_ai_query_cost_reducer";
import UpdateMcpQaReviewReducer from "./update_mcp_qa_review_reducer";
import UpdateQaEntryReducer from "./update_qa_entry_reducer";
import UpdateQaEntryWithPublishReducer from "./update_qa_entry_with_publish_reducer";
import UpsertQaFromCallReducer from "./upsert_qa_from_call_reducer";
// Import all procedure arg schemas
@ -124,6 +125,7 @@ const reducersSchema = __reducers(
__reducerSchema("update_ai_query_cost", UpdateAiQueryCostReducer),
__reducerSchema("update_mcp_qa_review", UpdateMcpQaReviewReducer),
__reducerSchema("update_qa_entry", UpdateQaEntryReducer),
__reducerSchema("update_qa_entry_with_publish", UpdateQaEntryWithPublishReducer),
__reducerSchema("upsert_qa_from_call", UpsertQaFromCallReducer),
);

View File

@ -22,6 +22,7 @@ import UnmarkHumanReviewedReducer from "../unmark_human_reviewed_reducer";
import UpdateAiQueryCostReducer from "../update_ai_query_cost_reducer";
import UpdateMcpQaReviewReducer from "../update_mcp_qa_review_reducer";
import UpdateQaEntryReducer from "../update_qa_entry_reducer";
import UpdateQaEntryWithPublishReducer from "../update_qa_entry_with_publish_reducer";
import UpsertQaFromCallReducer from "../upsert_qa_from_call_reducer";
export type AddManualQaParams = __Infer<typeof AddManualQaReducer>;
@ -40,5 +41,6 @@ export type UnmarkHumanReviewedParams = __Infer<typeof UnmarkHumanReviewedReduce
export type UpdateAiQueryCostParams = __Infer<typeof UpdateAiQueryCostReducer>;
export type UpdateMcpQaReviewParams = __Infer<typeof UpdateMcpQaReviewReducer>;
export type UpdateQaEntryParams = __Infer<typeof UpdateQaEntryReducer>;
export type UpdateQaEntryWithPublishParams = __Infer<typeof UpdateQaEntryWithPublishReducer>;
export type UpsertQaFromCallParams = __Infer<typeof UpsertQaFromCallReducer>;

View File

@ -0,0 +1,20 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
/* eslint-disable */
/* tslint:disable */
import {
TypeBuilder as __TypeBuilder,
t as __t,
type AlgebraicTypeType as __AlgebraicTypeType,
type Infer as __Infer,
} from "spacetimedb";
export default {
token: __t.string(),
qaId: __t.u64(),
question: __t.string(),
answer: __t.string(),
publish: __t.bool(),
editedBy: __t.string(),
};