Skip to content

Commit

Permalink
feat(user-controller): added option to return user related officers a…
Browse files Browse the repository at this point in the history
…nd deputies (#1955)

## Feature

Added option to return officers and ems deputies related to a User,
similar to the logic already in place for Citizens in endpoint
``v1/admin/manage/users/{id}``
  • Loading branch information
tugamars authored Sep 15, 2024
1 parent 5c00213 commit 6491543
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions apps/api/src/controllers/admin/manage/manage-users-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ import { getTranslator } from "~/utils/get-translator";
import { sendRawWebhook, sendDiscordWebhook } from "~/lib/discord/webhooks";
import { type APIEmbed } from "discord-api-types/v10";
import { getPrismaModelOrderBy } from "~/utils/order-by";
import { leoProperties, unitProperties } from "utils/leo/includes";

const manageUsersSelect = (selectCitizens: boolean) =>
const manageUsersSelect = (
selectCitizens: boolean = false,
selectOfficers: boolean = false,
selectDeputies: boolean = false,
) =>
({
...userProperties,
...(selectCitizens ? { citizens: { include: citizenInclude } } : {}),
...(selectOfficers ? { officers: { include: leoProperties } } : {}),
...(selectDeputies ? { emsFdDeputies: { include: unitProperties } } : {}),
apiToken: { include: { logs: { take: 35, orderBy: { createdAt: "desc" } } } },
roles: true,
User2FA: true,
Expand Down Expand Up @@ -194,13 +201,15 @@ export class ManageUsersController {
async getUserById(
@PathParams("id") id: string,
@QueryParams("select-citizens") selectCitizens: boolean,
@QueryParams("select-officers") selectOfficers: boolean,
@QueryParams("select-ems") selectDeputies: boolean,
@QueryParams("steamId", String) steamId?: string,
@QueryParams("discordId", String) discordId?: string,
): Promise<APITypes.GetManageUserByIdData | APITypes.GetManageUserByIdData[]> {
if (steamId || discordId) {
const users = await prisma.user.findMany({
where: { OR: [{ discordId }, { steamId }] },
select: manageUsersSelect(selectCitizens),
select: manageUsersSelect(selectCitizens, selectOfficers, selectDeputies),
});

if (users.length <= 0) {
Expand All @@ -218,7 +227,7 @@ export class ManageUsersController {

const user = await prisma.user.findFirst({
where: { OR: [{ id }, { discordId }, { steamId }] },
select: manageUsersSelect(selectCitizens),
select: manageUsersSelect(selectCitizens, selectOfficers, selectDeputies),
});

if (!user) {
Expand Down

0 comments on commit 6491543

Please sign in to comment.