forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-template.service.ts
More file actions
52 lines (42 loc) · 1.48 KB
/
Copy pathsplit-template.service.ts
File metadata and controls
52 lines (42 loc) · 1.48 KB
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
import { Injectable, NotFoundException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { SplitTemplate } from "./entities/split-template.entity";
import { Repository } from "typeorm";
import { CreateSplitTemplateDto } from "./dto/create-split-from-template.dto";
import { UpdateSplitTemplateDto } from "./dto/update-split-template.dto";
@Injectable()
export class SplitTemplateService {
constructor(
@InjectRepository(SplitTemplate)
private readonly repo: Repository<SplitTemplate>,
) {}
create(userId: string, dto: CreateSplitTemplateDto) {
const template = this.repo.create({ ...dto, userId });
return this.repo.save(template);
}
findAllForUser(userId: string) {
return this.repo.find({ where: { userId } });
}
findOne(id: string) {
return this.repo.findOneBy({ id });
}
update(id: string, dto: UpdateSplitTemplateDto) {
return this.repo.update(id, dto);
}
delete(id: string) {
return this.repo.delete(id);
}
async createSplitFromTemplate(templateId: string) {
const template = await this.findOne(templateId);
if (!template) throw new NotFoundException('Template not found');
// increment usage
await this.repo.increment({ id: templateId }, 'usageCount', 1);
return {
splitType: template.splitType,
participants: template.defaultParticipants,
items: template.defaultItems,
taxPercentage: template.taxPercentage,
tipPercentage: template.tipPercentage,
};
}
}