mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-19 11:12:00 +10:00
refactor(v4.0.0-alpha): beginning of a new era
This commit is contained in:
176
apps/server/src/resume/resume.service.ts
Normal file
176
apps/server/src/resume/resume.service.ts
Normal file
@ -0,0 +1,176 @@
|
||||
import { CACHE_MANAGER } from "@nestjs/cache-manager";
|
||||
import { Inject, Injectable, Logger } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { CreateResumeDto, ImportResumeDto, ResumeDto, UpdateResumeDto } from "@reactive-resume/dto";
|
||||
import { defaultResumeData, ResumeData } from "@reactive-resume/schema";
|
||||
import type { DeepPartial } from "@reactive-resume/utils";
|
||||
import { generateRandomName, kebabCase } from "@reactive-resume/utils";
|
||||
import { RedisService } from "@songkeys/nestjs-redis";
|
||||
import { Cache } from "cache-manager";
|
||||
import deepmerge from "deepmerge";
|
||||
import Redis from "ioredis";
|
||||
import { PrismaService } from "nestjs-prisma";
|
||||
|
||||
import { PrinterService } from "@/server/printer/printer.service";
|
||||
|
||||
import { StorageService } from "../storage/storage.service";
|
||||
import { UtilsService } from "../utils/utils.service";
|
||||
|
||||
@Injectable()
|
||||
export class ResumeService {
|
||||
private readonly redis: Redis;
|
||||
private readonly logger = new Logger(ResumeService.name);
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly printerService: PrinterService,
|
||||
private readonly storageService: StorageService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly utils: UtilsService,
|
||||
@Inject(CACHE_MANAGER) private readonly cache: Cache,
|
||||
) {
|
||||
this.redis = this.redisService.getClient();
|
||||
}
|
||||
|
||||
async create(userId: string, createResumeDto: CreateResumeDto) {
|
||||
const { name, email, picture } = await this.prisma.user.findUniqueOrThrow({
|
||||
where: { id: userId },
|
||||
select: { name: true, email: true, picture: true },
|
||||
});
|
||||
|
||||
const data = deepmerge(defaultResumeData, {
|
||||
basics: { name, email, picture: { url: picture ?? "" } },
|
||||
} satisfies DeepPartial<ResumeData>);
|
||||
|
||||
const resume = await this.prisma.resume.create({
|
||||
data: {
|
||||
data,
|
||||
userId,
|
||||
title: createResumeDto.title,
|
||||
visibility: createResumeDto.visibility,
|
||||
slug: createResumeDto.slug ?? kebabCase(createResumeDto.title),
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.all([
|
||||
this.cache.del(`user:${userId}:resumes`),
|
||||
this.cache.set(`user:${userId}:resume:${resume.id}`, resume),
|
||||
]);
|
||||
|
||||
return resume;
|
||||
}
|
||||
|
||||
async import(userId: string, importResumeDto: ImportResumeDto) {
|
||||
const randomTitle = generateRandomName();
|
||||
|
||||
const resume = await this.prisma.resume.create({
|
||||
data: {
|
||||
userId,
|
||||
visibility: "private",
|
||||
data: importResumeDto.data,
|
||||
title: importResumeDto.title || randomTitle,
|
||||
slug: importResumeDto.slug || kebabCase(randomTitle),
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.all([
|
||||
this.cache.del(`user:${userId}:resumes`),
|
||||
this.cache.set(`user:${userId}:resume:${resume.id}`, resume),
|
||||
]);
|
||||
|
||||
return resume;
|
||||
}
|
||||
|
||||
findAll(userId: string) {
|
||||
return this.utils.getCachedOrSet(`user:${userId}:resumes`, () =>
|
||||
this.prisma.resume.findMany({
|
||||
where: { userId },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
findOne(id: string, userId?: string) {
|
||||
if (userId) {
|
||||
return this.utils.getCachedOrSet(`user:${userId}:resume:${id}`, () =>
|
||||
this.prisma.resume.findUniqueOrThrow({
|
||||
where: { userId_id: { userId, id } },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return this.utils.getCachedOrSet(`user:public:resume:${id}`, () =>
|
||||
this.prisma.resume.findUniqueOrThrow({
|
||||
where: { id },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async findOneStatistics(userId: string, id: string) {
|
||||
const result = await Promise.all([
|
||||
this.redis.get(`user:${userId}:resume:${id}:views`),
|
||||
this.redis.get(`user:${userId}:resume:${id}:downloads`),
|
||||
]);
|
||||
|
||||
const [views, downloads] = result.map((value) => Number(value) || 0);
|
||||
|
||||
return { views, downloads };
|
||||
}
|
||||
|
||||
async findOneByUsernameSlug(username: string, slug: string) {
|
||||
const resume = await this.prisma.resume.findFirstOrThrow({
|
||||
where: { user: { username }, slug, visibility: "public" },
|
||||
});
|
||||
|
||||
// Update statistics: increment the number of views by 1
|
||||
await this.redis.incr(`user:${resume.userId}:resume:${resume.id}:views`);
|
||||
|
||||
return resume;
|
||||
}
|
||||
|
||||
async update(userId: string, id: string, updateResumeDto: UpdateResumeDto) {
|
||||
await Promise.all([
|
||||
this.cache.set(`user:${userId}:resume:${id}`, updateResumeDto),
|
||||
this.cache.del(`user:${userId}:resumes`),
|
||||
this.cache.del(`user:${userId}:storage:resumes:${id}`),
|
||||
this.cache.del(`user:${userId}:storage:previews:${id}`),
|
||||
]);
|
||||
|
||||
return this.prisma.resume.update({
|
||||
data: {
|
||||
title: updateResumeDto.title,
|
||||
slug: updateResumeDto.slug,
|
||||
visibility: updateResumeDto.visibility,
|
||||
data: updateResumeDto.data as unknown as Prisma.JsonObject,
|
||||
},
|
||||
where: { userId_id: { userId, id } },
|
||||
});
|
||||
}
|
||||
|
||||
async remove(userId: string, id: string) {
|
||||
await Promise.all([
|
||||
// Remove cached keys
|
||||
this.cache.del(`user:${userId}:resumes`),
|
||||
this.cache.del(`user:${userId}:resume:${id}`),
|
||||
|
||||
// Remove files in storage, and their cached keys
|
||||
this.storageService.deleteObject(userId, "resumes", id),
|
||||
this.storageService.deleteObject(userId, "previews", id),
|
||||
]);
|
||||
|
||||
return this.prisma.resume.delete({ where: { userId_id: { userId, id } } });
|
||||
}
|
||||
|
||||
async printResume(resume: ResumeDto) {
|
||||
const url = await this.printerService.printResume(resume);
|
||||
|
||||
// Update statistics: increment the number of downloads by 1
|
||||
await this.redis.incr(`user:${resume.userId}:resume:${resume.id}:downloads`);
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
printPreview(resume: ResumeDto) {
|
||||
return this.printerService.printPreview(resume);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user