Compare commits
2 Commits
9ac18c30b8
...
95068907f9
| Author | SHA1 | Date | |
|---|---|---|---|
|
95068907f9
|
|||
|
c00fbbaea2
|
@@ -1,9 +1,16 @@
|
||||
import { router, publicProcedure, protectedProcedure } from "./trpc";
|
||||
import { db } from "./db";
|
||||
import { imageToUser, opinion, user, userOpinion } from "./schema";
|
||||
import {
|
||||
imageToUser,
|
||||
opinion,
|
||||
province,
|
||||
user,
|
||||
userOpinion,
|
||||
zone,
|
||||
} from "./schema";
|
||||
import { createInsertSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
import { SQL, count, eq } 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";
|
||||
@@ -83,6 +90,7 @@ export const userRoute = router({
|
||||
group: z.number().optional(),
|
||||
zone: z.number().optional(),
|
||||
opinionCount: z.number().default(3),
|
||||
province: z.number().optional(),
|
||||
})
|
||||
)
|
||||
.query(
|
||||
@@ -92,18 +100,61 @@ export const userRoute = router({
|
||||
input.limit,
|
||||
input.opinionCount,
|
||||
input.group,
|
||||
input.zone
|
||||
input.zone,
|
||||
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,
|
||||
opinionLimit: number,
|
||||
group?: number,
|
||||
zone?: number
|
||||
zoneId?: number,
|
||||
provinceId?: number
|
||||
) {
|
||||
let zoneIds: number[] = await getZone(provinceId);
|
||||
if (provinceId && zoneIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
let users = await db.query.user.findMany({
|
||||
with: {
|
||||
group: true,
|
||||
@@ -121,8 +172,11 @@ async function getAllUser(
|
||||
if (group) {
|
||||
conditions.push(eq(user.group, group));
|
||||
}
|
||||
if (zone) {
|
||||
conditions.push(eq(user.zone, zone));
|
||||
if (zoneId) {
|
||||
conditions.push(eq(user.zone, zoneId));
|
||||
}
|
||||
if (zoneIds.length > 0) {
|
||||
conditions.push(inArray(user.zone, zoneIds));
|
||||
}
|
||||
return and(...conditions);
|
||||
},
|
||||
@@ -365,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