add direct invitation link
All checks were successful
deploy / build-and-deploy (push) Successful in 21s
All checks were successful
deploy / build-and-deploy (push) Successful in 21s
This commit is contained in:
@@ -25,6 +25,13 @@ CREATE TABLE IF NOT EXISTS blocked_user (
|
||||
comment TINYTEXT
|
||||
);
|
||||
|
||||
-- direct signup
|
||||
CREATE TABLE IF NOT EXISTS direct_signup (
|
||||
hash CHAR(255) NOT NULL UNIQUE,
|
||||
user_id INT,
|
||||
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- report
|
||||
CREATE TABLE IF NOT EXISTS report (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
|
||||
@@ -119,12 +119,26 @@ import {
|
||||
type GetReportAttachmentsReq,
|
||||
reportAttachment
|
||||
} from '@db/schema/reportAttachment.ts';
|
||||
import {
|
||||
addDirectSignup,
|
||||
type AddDirectSignupReq,
|
||||
deleteDirectSignup,
|
||||
type DeleteDirectSignupReq,
|
||||
directSignup,
|
||||
getDirectSignupByHash,
|
||||
type GetDirectSignupByHashReq,
|
||||
getDirectSignups,
|
||||
type GetDirectSignupsReq,
|
||||
setDirectSignupUser,
|
||||
type SetDirectSignupUserReq
|
||||
} from '@db/schema/directSignup.ts';
|
||||
|
||||
export class Database {
|
||||
protected readonly db: MySql2Database<{
|
||||
admin: typeof admin;
|
||||
user: typeof user;
|
||||
blockedUser: typeof blockedUser;
|
||||
directSignup: typeof directSignup;
|
||||
report: typeof report;
|
||||
reportAttachment: typeof reportAttachment;
|
||||
reportStatus: typeof reportStatus;
|
||||
@@ -149,6 +163,7 @@ export class Database {
|
||||
admin,
|
||||
user,
|
||||
blockedUser,
|
||||
directSignup,
|
||||
report,
|
||||
reportAttachment,
|
||||
reportStatus,
|
||||
@@ -191,6 +206,13 @@ export class Database {
|
||||
getBlockedUserByUuid = (values: GetBlockedUserByUuidReq) => getBlockedUserByUuid(this.db, values);
|
||||
getBlockedUsers = (values: GetBlockedUsersReq) => getBlockedUsers(this.db, values);
|
||||
|
||||
/* direct signup */
|
||||
addDirectSignup = (values: AddDirectSignupReq) => addDirectSignup(this.db, values);
|
||||
setDirectSignupUser = (values: SetDirectSignupUserReq) => setDirectSignupUser(this.db, values);
|
||||
deleteDirectSignup = (values: DeleteDirectSignupReq) => deleteDirectSignup(this.db, values);
|
||||
getDirectSignups = (values: GetDirectSignupsReq) => getDirectSignups(this.db, values);
|
||||
getDirectSignupByHash = (values: GetDirectSignupByHashReq) => getDirectSignupByHash(this.db, values);
|
||||
|
||||
/* report */
|
||||
addReport = (values: AddReportReq) => addReport(this.db, values);
|
||||
editReport = (values: EditReportReq) => editReport(this.db, values);
|
||||
|
||||
79
src/db/schema/directSignup.ts
Normal file
79
src/db/schema/directSignup.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import type { MySql2Database } from 'drizzle-orm/mysql2';
|
||||
import { int, mysqlTable, varchar } from 'drizzle-orm/mysql-core';
|
||||
import { user } from '@db/schema/user.ts';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { generateRandomString } from '@util/random.ts';
|
||||
import { BASE_PATH } from 'astro:env/server';
|
||||
|
||||
type Database = MySql2Database<{ directSignup: typeof directSignup }>;
|
||||
|
||||
export const directSignup = mysqlTable('direct_signup', {
|
||||
hash: varchar('hash', { length: 255 }).unique().notNull(),
|
||||
userId: int('user_id').references(() => user.id)
|
||||
});
|
||||
|
||||
export type AddDirectSignupReq = {};
|
||||
|
||||
export type SetDirectSignupUserReq = {
|
||||
hash: string;
|
||||
userId: number;
|
||||
};
|
||||
|
||||
export type DeleteDirectSignupReq = {
|
||||
hash: string;
|
||||
};
|
||||
|
||||
export type GetDirectSignupsReq = {};
|
||||
|
||||
export type GetDirectSignupByHashReq = {
|
||||
hash: string;
|
||||
};
|
||||
|
||||
export async function addDirectSignup(db: Database, _values: AddDirectSignupReq) {
|
||||
const hash = generateRandomString(16);
|
||||
|
||||
await db.insert(directSignup).values({
|
||||
hash: hash
|
||||
});
|
||||
|
||||
return { hash: hash, url: `${BASE_PATH}/signup/${hash}` };
|
||||
}
|
||||
|
||||
export async function setDirectSignupUser(db: Database, values: SetDirectSignupUserReq) {
|
||||
return db
|
||||
.update(directSignup)
|
||||
.set({
|
||||
userId: values.userId
|
||||
})
|
||||
.where(eq(directSignup.hash, values.hash));
|
||||
}
|
||||
|
||||
export async function deleteDirectSignup(db: Database, values: DeleteDirectSignupReq) {
|
||||
return db.delete(directSignup).where(eq(directSignup.hash, values.hash));
|
||||
}
|
||||
|
||||
export async function getDirectSignups(db: Database, _values: GetDirectSignupsReq) {
|
||||
const directSignups = await db
|
||||
.select({
|
||||
hash: directSignup.hash,
|
||||
user: user
|
||||
})
|
||||
.from(directSignup)
|
||||
.leftJoin(user, eq(directSignup.userId, user.id));
|
||||
|
||||
return directSignups.map((d) => ({ ...d, url: `${BASE_PATH}/signup/${d.hash}` }));
|
||||
}
|
||||
|
||||
export async function getDirectSignupByHash(db: Database, values: GetDirectSignupByHashReq) {
|
||||
const directSignups = await db
|
||||
.select({
|
||||
hash: directSignup.hash,
|
||||
user: user
|
||||
})
|
||||
.from(directSignup)
|
||||
.leftJoin(user, eq(directSignup.userId, user.id))
|
||||
.where(eq(directSignup.hash, values.hash))
|
||||
.limit(1);
|
||||
|
||||
return directSignups[0] ?? null;
|
||||
}
|
||||
Reference in New Issue
Block a user