-
Notifications
You must be signed in to change notification settings - Fork 5
/
ResumeService.ts
80 lines (68 loc) · 2.78 KB
/
ResumeService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { ForbiddenError, NotFoundError } from 'routing-controllers';
import { Service } from 'typedi';
import { EntityManager } from 'typeorm';
import { InjectManager } from 'typeorm-typedi-extensions';
import { ActivityType, ResumePatches } from '../types';
import { ResumeModel } from '../models/ResumeModel';
import { UserModel } from '../models/UserModel';
import Repositories, { TransactionsManager } from '../repositories';
@Service()
export default class ResumeService {
private transactions: TransactionsManager;
constructor(@InjectManager() entityManager: EntityManager) {
this.transactions = new TransactionsManager(entityManager);
}
public async getVisibleResumes() : Promise<ResumeModel[]> {
const resumes = await this.transactions.readOnly(async (txn) => Repositories
.resume(txn)
.findVisibleResumes());
return resumes;
}
public async updateResume(user: UserModel, resumeURL: string, isResumeVisible: boolean): Promise<ResumeModel> {
return this.transactions.readWrite(async (txn) => {
const resumeRepository = Repositories.resume(txn);
const oldResume = await resumeRepository.findByUserUuid(user.uuid);
if (oldResume) await resumeRepository.deleteResume(oldResume);
const resume = await resumeRepository.upsertResume(ResumeModel.create({
user,
isResumeVisible,
url: resumeURL,
}));
await Repositories.activity(txn).logActivity({
type: ActivityType.RESUME_UPLOAD,
user,
});
return resume;
});
}
public async patchResume(uuid: string, patches: ResumePatches, user: UserModel) {
return this.transactions.readWrite(async (txn) => {
const resumeRepository = Repositories.resume(txn);
const resume = await resumeRepository.findByUuid(uuid);
if (!resume) throw new NotFoundError('Resume not found');
if (resume.user.uuid !== user.uuid) {
throw new ForbiddenError('Cannot update a resume of another user');
}
return resumeRepository.upsertResume(resume, patches);
});
}
public async getUserResume(user: UserModel): Promise<ResumeModel> {
return this.transactions.readWrite(async (txn) => Repositories
.resume(txn)
.findByUserUuid(user.uuid));
}
public async deleteResume(uuid: string, user: UserModel): Promise<ResumeModel> {
return this.transactions.readWrite(async (txn) => {
const resumeRepository = Repositories.resume(txn);
const resume = await resumeRepository.findByUuid(uuid);
if (!resume) throw new NotFoundError('Resume not found');
if (resume.user.uuid !== user.uuid) {
throw new ForbiddenError(
'Cannot delete a resume that belongs to another user',
);
}
await resumeRepository.deleteResume(resume);
return resume;
});
}
}