import { User } from "../models/User";
export class UserService {
async all() {
return new User().all();
}
async find(id: number | string) {
return User.find(id);
}
async findByEmail(email: string) {
return User.findOneBy("email", email);
}
async recentActive(limit = 20) {
return User.where("is_active", true)
.orderBy("created_at", "desc")
.limit(limit)
.get();
}
async findWithPosts(id: number | string) {
const user = await User.find(id);
if (!user) return null;
await (user as { load?: (...relations: string[]) => Promise<unknown> }).load?.("posts");
return user;
}
async create(data: Record<string, unknown>) {
return User.create(data);
}
async createMany(rows: Record<string, unknown>[]) {
return User.createMany(rows);
}
async update(id: number | string, data: Record<string, unknown>) {
const user = await User.find(id);
if (!user) return null;
user.update(data);
await user.save();
return user;
}
async delete(id: number | string) {
return User.deleteById(id);
}
async restore(id: number | string) {
return User.restoreById(id);
}
async deactivateMany(ids: Array<number | string>) {
await User.updateMany(ids, { status: "inactive" });
}
}