feat: add modular services and repositories for improved code organization
Introduce repository pattern across multiple services, including `favorites`, `tags`, `sessions`, `reports`, `auth`, and more. Decouple crypto functionalities into modular services like `HashingService`, `JwtService`, and `EncryptionService`. Improve testability and maintainability by simplifying dependencies and consolidating utility logic.
This commit is contained in:
158
backend/src/users/repositories/users.repository.ts
Normal file
158
backend/src/users/repositories/users.repository.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { and, eq, lte, sql } from "drizzle-orm";
|
||||
import { DatabaseService } from "../../database/database.service";
|
||||
import { users, contents, favorites } from "../../database/schemas";
|
||||
import type { UpdateUserDto } from "../dto/update-user.dto";
|
||||
|
||||
@Injectable()
|
||||
export class UsersRepository {
|
||||
constructor(private readonly databaseService: DatabaseService) {}
|
||||
|
||||
async create(data: {
|
||||
username: string;
|
||||
email: string;
|
||||
passwordHash: string;
|
||||
emailHash: string;
|
||||
}) {
|
||||
const [newUser] = await this.databaseService.db
|
||||
.insert(users)
|
||||
.values(data)
|
||||
.returning();
|
||||
return newUser;
|
||||
}
|
||||
|
||||
async findByEmailHash(emailHash: string) {
|
||||
const result = await this.databaseService.db
|
||||
.select({
|
||||
uuid: users.uuid,
|
||||
username: users.username,
|
||||
email: users.email,
|
||||
passwordHash: users.passwordHash,
|
||||
status: users.status,
|
||||
isTwoFactorEnabled: users.isTwoFactorEnabled,
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.emailHash, emailHash))
|
||||
.limit(1);
|
||||
return result[0] || null;
|
||||
}
|
||||
|
||||
async findOneWithPrivateData(uuid: string) {
|
||||
const result = await this.databaseService.db
|
||||
.select({
|
||||
uuid: users.uuid,
|
||||
username: users.username,
|
||||
email: users.email,
|
||||
displayName: users.displayName,
|
||||
status: users.status,
|
||||
isTwoFactorEnabled: users.isTwoFactorEnabled,
|
||||
createdAt: users.createdAt,
|
||||
updatedAt: users.updatedAt,
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.uuid, uuid))
|
||||
.limit(1);
|
||||
return result[0] || null;
|
||||
}
|
||||
|
||||
async countAll() {
|
||||
const result = await this.databaseService.db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(users);
|
||||
return Number(result[0].count);
|
||||
}
|
||||
|
||||
async findAll(limit: number, offset: number) {
|
||||
return await this.databaseService.db
|
||||
.select({
|
||||
uuid: users.uuid,
|
||||
username: users.username,
|
||||
displayName: users.displayName,
|
||||
status: users.status,
|
||||
createdAt: users.createdAt,
|
||||
})
|
||||
.from(users)
|
||||
.limit(limit)
|
||||
.offset(offset);
|
||||
}
|
||||
|
||||
async findByUsername(username: string) {
|
||||
const result = await this.databaseService.db
|
||||
.select({
|
||||
uuid: users.uuid,
|
||||
username: users.username,
|
||||
displayName: users.displayName,
|
||||
createdAt: users.createdAt,
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.username, username))
|
||||
.limit(1);
|
||||
return result[0] || null;
|
||||
}
|
||||
|
||||
async findOne(uuid: string) {
|
||||
const result = await this.databaseService.db
|
||||
.select()
|
||||
.from(users)
|
||||
.where(eq(users.uuid, uuid))
|
||||
.limit(1);
|
||||
return result[0] || null;
|
||||
}
|
||||
|
||||
async update(uuid: string, data: any) {
|
||||
return await this.databaseService.db
|
||||
.update(users)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(users.uuid, uuid))
|
||||
.returning();
|
||||
}
|
||||
|
||||
async getTwoFactorSecret(uuid: string) {
|
||||
const result = await this.databaseService.db
|
||||
.select({
|
||||
secret: users.twoFactorSecret,
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.uuid, uuid))
|
||||
.limit(1);
|
||||
return result[0]?.secret || null;
|
||||
}
|
||||
|
||||
async getUserContents(uuid: string) {
|
||||
return await this.databaseService.db
|
||||
.select()
|
||||
.from(contents)
|
||||
.where(eq(contents.userId, uuid));
|
||||
}
|
||||
|
||||
async getUserFavorites(uuid: string) {
|
||||
return await this.databaseService.db
|
||||
.select()
|
||||
.from(favorites)
|
||||
.where(eq(favorites.userId, uuid));
|
||||
}
|
||||
|
||||
async softDeleteUserAndContents(uuid: string) {
|
||||
return await this.databaseService.db.transaction(async (tx) => {
|
||||
const userResult = await tx
|
||||
.update(users)
|
||||
.set({ status: "deleted", deletedAt: new Date() })
|
||||
.where(eq(users.uuid, uuid))
|
||||
.returning();
|
||||
|
||||
await tx
|
||||
.update(contents)
|
||||
.set({ deletedAt: new Date() })
|
||||
.where(eq(contents.userId, uuid));
|
||||
|
||||
return userResult;
|
||||
});
|
||||
}
|
||||
|
||||
async purgeDeleted(before: Date) {
|
||||
return await this.databaseService.db
|
||||
.delete(users)
|
||||
.where(and(eq(users.status, "deleted"), lte(users.deletedAt, before)))
|
||||
.returning();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user