Gacha game centered around vore.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
vore-gacha/src/queries/UserManager.ts

133 lines
4.0 KiB

import {DiscordUser, Prisma, PrismaClient} from "./Prisma.js";
import {Snowflake} from "discord-api-types";
import cuid from "cuid";
const userRegistrationSelect = {
id: true,
name: true,
discordUser: true,
gender: true,
joinedAt: true,
} as const
export type UserRegistrationData = Prisma.UserGetPayload<{ select: typeof userRegistrationSelect }>
const genderListSelect = {
id: true,
name: true,
} as const
export type GenderListData = Prisma.GenderGetPayload<{ select: typeof genderListSelect }>
export class UserManager {
readonly client: PrismaClient
constructor(client: PrismaClient) {
this.client = client
}
async registerOrUpdateDiscordUser({
discordId,
username,
discriminator,
}: { discordId: Snowflake, username: string, discriminator: string }): Promise<DiscordUser> {
return (await this.client.discordUser.upsert({
where: {
discordId,
},
create: {
discordId,
username,
discriminator,
userId: null,
},
update: {
username,
discriminator,
user: {
update: {
lastActive: new Date()
}
}
},
include: {
user: true,
}
}))
}
async registerOrReregisterUserFromDiscord({
discordId,
username,
discriminator,
name,
genderId
}: { discordId: Snowflake, username: string, discriminator: string, name: string, genderId: string }): Promise<{
user: UserRegistrationData, created: boolean
}> {
const userId = cuid()
const user = (await this.client.discordUser.upsert({
where: {
discordId,
},
update: {
username,
discriminator,
user: {
upsert: {
update: {
name,
gender: {
connect: {
id: genderId,
}
},
lastActive: new Date()
},
create: {
id: userId,
name,
gender: {
connect: {
id: genderId,
}
},
}
}
}
},
create: {
discordId,
username,
discriminator,
user: {
create: {
id: userId,
name,
gender: {
connect: {
id: genderId,
}
},
}
}
},
select: {
user: {
select: userRegistrationSelect
}
},
})).user
if (user === null) {
throw Error("...Somehow, there wasn't a user to return?!")
}
return {
user,
created: user.id === userId,
}
}
async getGenders(): Promise<GenderListData[]> {
return this.client.gender.findMany({
select: genderListSelect
})
}
}