| [b69759d] | 1 | import * as drizzleQueries from "../../../database/drizzle/queries";
|
|---|
| [958bc89] | 2 | import {requireAdmin} from "../../../server/telefunc/ctx";
|
|---|
| [b69759d] | 3 | import {Abort} from "telefunc";
|
|---|
| [5750945] | 4 | import { validateComponentSpecificData } from "../../../database/drizzle/util/componentFieldConfig";
|
|---|
| [b69759d] | 5 |
|
|---|
| 6 | export async function getAdminInfoAndData() {
|
|---|
| 7 | const { c, userId } = await requireAdmin();
|
|---|
| 8 |
|
|---|
| 9 | const admin = await drizzleQueries.getUserProfile(c.db, userId);
|
|---|
| 10 |
|
|---|
| 11 | if (!admin) throw new Error();
|
|---|
| 12 |
|
|---|
| 13 | const pendingBuilds = await drizzleQueries.getPendingBuilds(c.db);
|
|---|
| 14 | const userBuilds = await drizzleQueries.getUserBuilds(c.db, userId);
|
|---|
| [5ce5904] | 15 | const componentSuggestions = await drizzleQueries.getComponentSuggestions(c.db);
|
|---|
| [b69759d] | 16 |
|
|---|
| 17 | return {
|
|---|
| 18 | admin,
|
|---|
| 19 | pendingBuilds,
|
|---|
| 20 | userBuilds,
|
|---|
| [5ce5904] | 21 | componentSuggestions
|
|---|
| [b69759d] | 22 | };
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| [226fbbb] | 25 | export async function onSetBuildApprovalStatus({ buildId, isApproved }
|
|---|
| [b69759d] | 26 | : { buildId: number; isApproved: boolean }) {
|
|---|
| 27 | const { c, userId } = await requireAdmin()
|
|---|
| 28 |
|
|---|
| 29 | if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 30 |
|
|---|
| 31 | const result = await drizzleQueries.setBuildApprovalStatus(c.db, buildId, isApproved);
|
|---|
| 32 |
|
|---|
| 33 | if (!result) throw Abort();
|
|---|
| 34 |
|
|---|
| 35 | return { success: true };
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| [226fbbb] | 38 | export async function onSetComponentSuggestionStatus({ suggestionId, status, adminComment }
|
|---|
| [b69759d] | 39 | : { suggestionId: number; status: string; adminComment: string }) {
|
|---|
| 40 | const { c, userId } = await requireAdmin()
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| [5ce5904] | 43 | if(!Number.isInteger(suggestionId) || suggestionId <= 0) throw Abort();
|
|---|
| 44 |
|
|---|
| 45 | const result = await drizzleQueries.setComponentSuggestionStatus(c.db, suggestionId, userId, status, adminComment);
|
|---|
| 46 |
|
|---|
| 47 | if (!result) throw Abort();
|
|---|
| 48 |
|
|---|
| 49 | return { success: true };
|
|---|
| [226fbbb] | 50 | }
|
|---|
| 51 |
|
|---|
| 52 | export async function onCreateNewComponent({ name, brand, price, imgUrl, type, specificData }
|
|---|
| 53 | : { name: string; brand: string; price: number; imgUrl: string; type: string, specificData: any }) {
|
|---|
| 54 |
|
|---|
| 55 | const { c, userId } = await requireAdmin()
|
|---|
| 56 |
|
|---|
| 57 | if(!validateComponentSpecificData(type, specificData)) throw Abort();
|
|---|
| 58 |
|
|---|
| 59 | const newComponentId = await drizzleQueries.addNewComponent(c.db, name, brand, price, imgUrl, type, specificData);
|
|---|
| 60 |
|
|---|
| 61 | if(!newComponentId) throw Abort();
|
|---|
| 62 |
|
|---|
| [83fb5e2] | 63 | return { success: true };
|
|---|
| [226fbbb] | 64 | }
|
|---|
| [dda6f51] | 65 |
|
|---|
| 66 | export async function onGetDetailsForNewComponent({ type }
|
|---|
| 67 | : { type: string }) {
|
|---|
| 68 | const { c, userId } = await requireAdmin()
|
|---|
| 69 |
|
|---|
| [be22289] | 70 | const details = await drizzleQueries.getDetailsNewComponent(type);
|
|---|
| [dda6f51] | 71 |
|
|---|
| 72 | return details;
|
|---|
| 73 | }
|
|---|