Dough Git

clove/pkviewer

web/src/lib/admin.remote.ts

back to web/src/lib

/* web/src/lib/admin.remote.ts */
import { command } from "$app/server";
import { manageApi, seg } from "$lib/server/api.ts";
import type { ActionResult, BadgeDraft } from "$lib/types.ts";

/**
 * Remote commands for the administration UI.
 *
 * Every one forwards to the API, which re-checks the platform admin grant.
 * Nothing here decides whether the caller is an admin.
 */

function messageFor(error: string, status: number): string {
  switch (error) {
    case "unauthenticated":
      return "Your session has expired. Sign in again.";
    case "not_found":
      return "Not found.";
    case "unknown_subject":
      return "No system on pkviewer matches that address or ID. It has to be claimed first.";
    case "unknown_badge":
      return "That badge no longer exists.";
    case "badge_retired":
      return "That badge is retired and cannot be granted.";
    case "not_empty":
      return "Move or delete the entries in that section first.";
    case "invalid":
      return "Some values were not accepted. Check them and try again.";
    case "api_unreachable":
      return "Could not reach pkviewer. Check your connection and try again.";
    default:
      return status >= 500 ? "Something went wrong. Please try again." : "That did not work.";
  }
}

function done(response: { ok: true } | { ok: false; error: string; status: number }): ActionResult {
  return response.ok
    ? { ok: true }
    : { ok: false, error: messageFor(response.error, response.status) };
}

// ------------------------------------------------------------------ badges

export const grantBadge = command(
  "unchecked",
  async (input: { subject: string; badgeId: string; note: string }) =>
    done(
      await manageApi.post("/admin/assignments", {
        subject: input.subject,
        badgeId: input.badgeId,
        note: input.note || null,
      }),
    ),
);

export const revokeBadge = command("unchecked", async (assignmentId: number) =>
  done(await manageApi.post(`/admin/assignments/${seg(assignmentId)}/revoke`, {})),
);

export const saveBadge = command("unchecked", async (input: BadgeDraft) =>
  done(await manageApi.put(`/admin/badges/${seg(input.id)}`, input)),
);

export const retireBadge = command(
  "unchecked",
  async (input: { badgeId: string; retired: boolean }) =>
    done(
      await manageApi.post(
        `/admin/badges/${seg(input.badgeId)}/${input.retired ? "retire" : "restore"}`,
        {},
      ),
    ),
);

// ----------------------------------------------------------------- credits

export const saveCredit = command(
  "unchecked",
  async (input: {
    id?: string;
    sectionId: string;
    name: string;
    detail: string;
    url: string;
    sortOrder: number;
    visible: boolean;
  }) => {
    const body = {
      sectionId: input.sectionId,
      name: input.name,
      detail: input.detail || null,
      url: input.url || null,
      sortOrder: input.sortOrder,
      visible: input.visible,
    };
    return done(
      input.id
        ? await manageApi.put(`/admin/credits/${seg(input.id)}`, body)
        : await manageApi.post("/admin/credits", body),
    );
  },
);

export const deleteCredit = command("unchecked", async (id: string) =>
  done(await manageApi.del(`/admin/credits/${seg(id)}`)),
);

export const saveSection = command(
  "unchecked",
  async (input: { id: string; label: string; description: string; sortOrder: number }) =>
    done(
      await manageApi.put(`/admin/credits/sections/${seg(input.id)}`, {
        label: input.label,
        description: input.description || null,
        sortOrder: input.sortOrder,
      }),
    ),
);

export const deleteSection = command("unchecked", async (id: string) =>
  done(await manageApi.del(`/admin/credits/sections/${seg(id)}`)),
);