import { mutation, t, table } from "@bijectionhq/bijection";
import { support } from "./agents";
import { desk } from "./access";
export const ratings = table(
{ turn: t.string, by: t.principal, helpful: t.boolean, comment: t.maybe(t.string) },
{ key: ["turn", "by"], label: desk },
);
export const rate = mutation(
{
under: desk,
allow: [support.participant],
args: { turn: t.string, helpful: t.boolean, comment: t.maybe(t.string) },
refuses: { NoTurn: null },
},
async (ctx, { turn, helpful, comment }) => {
const own = await ctx.call(support.turns.get, { id: turn });
if (own === null || !["completed", "failed", "stopped"].includes(own.state)) {
ctx.refuse("NoTurn", "No settled turn of yours");
}
await ctx.insert(ratings({ turn, by: ctx.frame.as.id }), { helpful, comment });
return null;
},
);