This commit is contained in:
2026-05-16 18:57:42 +07:00
commit 01c00093d5
128 changed files with 23193 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
import { eq, or } from "drizzle-orm";
import * as schema from "@db/schema";
import type { InsertUser } from "@db/schema";
import { getDb } from "./connection";
import { env } from "../lib/env";
export async function findUserByUnionId(unionId: string) {
const rows = await getDb()
.select()
.from(schema.users)
.where(eq(schema.users.unionId, unionId))
.limit(1);
return rows.at(0);
}
export async function findUserByLoginIdentifier(identifier: string) {
const rows = await getDb()
.select()
.from(schema.users)
.where(or(eq(schema.users.unionId, identifier), eq(schema.users.email, identifier)))
.limit(1);
return rows.at(0);
}
export async function touchUserLastSignIn(unionId: string) {
await getDb()
.update(schema.users)
.set({ lastSignInAt: new Date() })
.where(eq(schema.users.unionId, unionId));
}
export async function upsertUser(data: InsertUser) {
const values = { ...data };
const updateSet: Partial<InsertUser> = {
lastSignInAt: new Date(),
...data,
};
if (
values.role === undefined &&
values.unionId &&
values.unionId === env.ownerUnionId
) {
values.role = "admin";
updateSet.role = "admin";
}
await getDb()
.insert(schema.users)
.values(values)
.onDuplicateKeyUpdate({ set: updateSet });
}