added getAllUserCount
All checks were successful
backend-action / build-image (push) Successful in 28s
All checks were successful
backend-action / build-image (push) Successful in 28s
This commit is contained in:
@@ -10,7 +10,7 @@ import {
|
||||
} from "./schema";
|
||||
import { createInsertSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
import { SQL, count, eq, inArray } from "drizzle-orm";
|
||||
import { SQL, and, count, eq, inArray } from "drizzle-orm";
|
||||
import { Config } from "./config";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import * as jwt from "jsonwebtoken";
|
||||
@@ -104,8 +104,45 @@ export const userRoute = router({
|
||||
input.province
|
||||
)
|
||||
),
|
||||
getAllUserCount: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
group: z.number().optional(),
|
||||
zone: z.number().optional(),
|
||||
province: z.number().optional(),
|
||||
})
|
||||
)
|
||||
.query(
|
||||
async ({ input }) =>
|
||||
await getAllUserCount(input.group, input.zone, input.province)
|
||||
),
|
||||
});
|
||||
|
||||
async function getAllUserCount(
|
||||
group?: number,
|
||||
zoneId?: number,
|
||||
provinceId?: number
|
||||
) {
|
||||
let zoneIds: number[] = await getZone(provinceId);
|
||||
if (provinceId && zoneIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
let conditions: SQL[] = [];
|
||||
if (group) {
|
||||
conditions.push(eq(user.group, group));
|
||||
}
|
||||
if (zoneId) {
|
||||
conditions.push(eq(user.zone, zoneId));
|
||||
}
|
||||
if (zoneIds.length > 0) {
|
||||
conditions.push(inArray(user.zone, zoneIds));
|
||||
}
|
||||
return await db
|
||||
.select({ count: count(user.id) })
|
||||
.from(user)
|
||||
.where(and(...conditions))
|
||||
.then((v) => v[0].count);
|
||||
}
|
||||
async function getAllUser(
|
||||
offset: number,
|
||||
limit: number,
|
||||
@@ -114,16 +151,9 @@ async function getAllUser(
|
||||
zoneId?: number,
|
||||
provinceId?: number
|
||||
) {
|
||||
let zoneIds: number[] = [];
|
||||
if (provinceId) {
|
||||
zoneIds = await db
|
||||
.select({ id: zone.id })
|
||||
.from(zone)
|
||||
.where(eq(zone.province, provinceId))
|
||||
.then((queryResult) => queryResult.map((v) => v.id));
|
||||
if (zoneIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
let zoneIds: number[] = await getZone(provinceId);
|
||||
if (provinceId && zoneIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
let users = await db.query.user.findMany({
|
||||
with: {
|
||||
@@ -389,3 +419,15 @@ function createJWT(phone: string) {
|
||||
function generateRandomString() {
|
||||
return Math.random().toString(36).substring(2, 15);
|
||||
}
|
||||
|
||||
async function getZone(province?: number) {
|
||||
if (province === undefined) {
|
||||
return [];
|
||||
}
|
||||
const zoneIds = await db
|
||||
.select({ id: zone.id })
|
||||
.from(zone)
|
||||
.where(eq(zone.province, province))
|
||||
.then((queryResult) => queryResult.map((v) => v.id));
|
||||
return zoneIds;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user