| [b69759d] | 1 | import * as drizzleQueries from "../database/drizzle/queries";
|
|---|
| 2 | import {ctx, getAuthState, requireUser} from "../server/telefunc/ctx";
|
|---|
| 3 | import {Abort} from "telefunc";
|
|---|
| 4 |
|
|---|
| 5 | export async function getAuthenticationState() {
|
|---|
| [5ce5904] | 6 | const context = getAuthState();
|
|---|
| [b69759d] | 7 |
|
|---|
| [5ce5904] | 8 | return context;
|
|---|
| [b69759d] | 9 | }
|
|---|
| 10 |
|
|---|
| [5ce5904] | 11 | export async function getPopupAllComponents({ componentType, limit, q }
|
|---|
| 12 | : { componentType?: string; limit?: number; q?: string }) {
|
|---|
| [b69759d] | 13 | const c = ctx();
|
|---|
| 14 |
|
|---|
| [5ce5904] | 15 | const components = await drizzleQueries.getAllComponents(c.db, limit, q, componentType);
|
|---|
| 16 |
|
|---|
| 17 | return components;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | export async function getPopupComponentDetails({ componentId }
|
|---|
| 21 | : { componentId: number }) {
|
|---|
| 22 | const context = ctx();
|
|---|
| 23 |
|
|---|
| 24 | if(!Number.isInteger(componentId) || componentId <= 0) throw Abort();
|
|---|
| 25 |
|
|---|
| 26 | const componentDetails = await drizzleQueries.getComponentDetails(context.db, componentId);
|
|---|
| 27 |
|
|---|
| 28 | if(!componentDetails) throw Abort();
|
|---|
| 29 |
|
|---|
| 30 | return componentDetails;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | export async function getPopupSuggestComponent({ link, description, componentType }
|
|---|
| 34 | : { link: string; description: string; componentType: string }) {
|
|---|
| 35 | const { c, userId } = requireUser()
|
|---|
| 36 |
|
|---|
| 37 | const newSuggestionId = await drizzleQueries.addComponentSuggestion(c.db, userId, link, description, componentType);
|
|---|
| 38 |
|
|---|
| 39 | if(!newSuggestionId) throw Abort();
|
|---|
| 40 |
|
|---|
| 41 | return newSuggestionId;
|
|---|
| [b69759d] | 42 | }
|
|---|
| 43 |
|
|---|
| 44 | export async function getPopupAllApprovedBuilds({ limit, q }
|
|---|
| 45 | : { limit?: number; q?: string }) {
|
|---|
| 46 | const context = ctx();
|
|---|
| 47 |
|
|---|
| 48 | const approvedBuilds = await drizzleQueries.getApprovedBuilds(context.db, limit, q);
|
|---|
| 49 |
|
|---|
| 50 | return approvedBuilds;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | export async function getPopupHighestRankedBuilds({ limit }
|
|---|
| 54 | : { limit?: number }) {
|
|---|
| 55 | const context = ctx();
|
|---|
| 56 |
|
|---|
| 57 | const highestRankedBuilds = await drizzleQueries.getHighestRankedBuilds(context.db, limit);
|
|---|
| 58 |
|
|---|
| 59 | return highestRankedBuilds;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | // Shared
|
|---|
| 63 |
|
|---|
| 64 | export async function getPopupBuildDetails({ buildId }
|
|---|
| 65 | : { buildId: number }) {
|
|---|
| [5ce5904] | 66 | const context = getAuthState();
|
|---|
| [b69759d] | 67 |
|
|---|
| 68 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 69 |
|
|---|
| [5ce5904] | 70 | const buildDetails = await drizzleQueries.getBuildDetails(context.db, buildId, context.userId ?? undefined);
|
|---|
| [b69759d] | 71 |
|
|---|
| 72 | if(!buildDetails) throw Abort();
|
|---|
| 73 |
|
|---|
| 74 | return buildDetails;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | export async function togglePopupFavorite({ buildId }
|
|---|
| 78 | : { buildId: number }) {
|
|---|
| 79 | const { c, userId } = requireUser()
|
|---|
| 80 |
|
|---|
| 81 | if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 82 |
|
|---|
| 83 | const isFavorite = await drizzleQueries.toggleFavoriteBuild(c.db, userId, buildId);
|
|---|
| 84 |
|
|---|
| 85 | return isFavorite.favorite;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | export async function setPopupRating({ buildId, value }
|
|---|
| 89 | : { buildId: number; value: number }) {
|
|---|
| 90 | const { c, userId } = requireUser()
|
|---|
| 91 |
|
|---|
| 92 | if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 93 |
|
|---|
| [5ce5904] | 94 | if (!Number.isInteger(value) || value < 1 || value > 5) throw Abort();
|
|---|
| 95 |
|
|---|
| [b69759d] | 96 | const result = await drizzleQueries.setBuildRating(c.db, userId, buildId, value);
|
|---|
| 97 |
|
|---|
| 98 | return { success: true };
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | export async function setPopupReview({ buildId, content }
|
|---|
| 102 | : { buildId: number; content: string }) {
|
|---|
| 103 | const { c, userId } = requireUser()
|
|---|
| 104 |
|
|---|
| 105 | if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 106 |
|
|---|
| 107 | const result = await drizzleQueries.setBuildReview(c.db, userId, buildId, content);
|
|---|
| 108 |
|
|---|
| 109 | return { success: true };
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | export async function clonePopupBuild({ buildId }
|
|---|
| 113 | : { buildId: number }) {
|
|---|
| 114 | const { c, userId } = requireUser()
|
|---|
| 115 |
|
|---|
| 116 | if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 117 |
|
|---|
| 118 | const newBuildId = await drizzleQueries.cloneBuild(c.db, userId, buildId);
|
|---|
| 119 |
|
|---|
| 120 | if (!newBuildId) throw Abort();
|
|---|
| 121 |
|
|---|
| 122 | return newBuildId;
|
|---|
| 123 | }
|
|---|