Skip to content

Commit

Permalink
Improve role task queries
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Aug 14, 2024
1 parent 8f328de commit 84d04b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/lib/rawSql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Prisma } from '@prisma/client';
import { logError } from './util/logError';

const u = Prisma.UserScalarFieldEnum;

Expand All @@ -14,3 +15,14 @@ SET ${u.cl_array} = (
)
WHERE ${u.id} = '${userID}';`
};

export async function loggedRawPrismaQuery<T>(query: string): Promise<T | null> {
try {
const result = await prisma.$queryRawUnsafe<T>(query);
return result;
} catch (err) {
logError(err, { query: query.slice(0, 100) });
}

return null;
}
9 changes: 5 additions & 4 deletions src/lib/rolesTask.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noOp, uniqueArr } from 'e';
import { noOp, notEmpty, uniqueArr } from 'e';

import { SupportServer } from '../config';
import { BadgesEnum, Roles } from '../lib/constants';
Expand All @@ -11,6 +11,7 @@ import { partition } from 'remeda';
import z from 'zod';
import { type CommandResponse, Stopwatch, convertXPtoLVL, getUsernameSync, returnStringOrFile } from '../lib/util';
import { ClueTiers } from './clues/clueTiers';
import { loggedRawPrismaQuery } from './rawSql';
import { TeamLoot } from './simulation/TeamLoot';
import { SkillsArray } from './skilling/types';
import type { ItemBank } from './types';
Expand Down Expand Up @@ -385,8 +386,8 @@ export async function runRolesTask(dryRun: boolean): Promise<CommandResponse> {

debugLog(`Finished role functions, ${results.length} results`);

const allBadgeIDs = uniqueArr(results.map(i => i.badge));
const allRoleIDs = uniqueArr(results.map(i => i.roleID));
const allBadgeIDs = uniqueArr(results.map(i => i.badge)).filter(notEmpty);
const allRoleIDs = uniqueArr(results.map(i => i.roleID)).filter(notEmpty);

if (!dryRun) {
const roleNames = new Map<string, string>();
Expand All @@ -396,7 +397,7 @@ export async function runRolesTask(dryRun: boolean): Promise<CommandResponse> {
// Remove all top badges from all users (and add back later)
debugLog('Removing badges...');
const badgeIDs = `ARRAY[${allBadgeIDs.join(',')}]`;
await prisma.$queryRawUnsafe(`
await loggedRawPrismaQuery(`
UPDATE users
SET badges = badges - ${badgeIDs}
WHERE badges && ${badgeIDs}
Expand Down

0 comments on commit 84d04b0

Please sign in to comment.