This repository was archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromotions.service.ts
More file actions
150 lines (121 loc) · 5.21 KB
/
Copy pathpromotions.service.ts
File metadata and controls
150 lines (121 loc) · 5.21 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { join } from 'path';
import { MikroORM, CreateRequestContext } from '@mikro-orm/core';
import { Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { env } from '@env';
import { OutputMessageDTO } from '@modules/base/dto/output.dto';
import { i18nNotFoundException } from '@modules/base/http-errors';
import { OutputFileDTO } from '@modules/files/dto/output.dto';
import { ImagesService } from '@modules/files/images.service';
import { OutputPromotionDTO } from './dto/output.dto';
import { PromotionPicture } from './entities/promotion-picture.entity';
import { Promotion } from './entities/promotion.entity';
import { OutputBaseUserDTO } from '../users/dto/output.dto';
@Injectable()
export class PromotionsService {
constructor(private readonly orm: MikroORM, private readonly imagesService: ImagesService) {}
/**
* Create a new promotion each year on the 15th of July
*/
@Cron('0 0 0 15 7 *')
@CreateRequestContext()
async createNewPromotion(): Promise<void> {
const latest = await this.findLatest();
const newPromotion = this.orm.em.create(Promotion, { number: latest.number + 1 });
await this.orm.em.persistAndFlush(newPromotion);
}
@CreateRequestContext()
async findAll(): Promise<OutputPromotionDTO[]> {
return (await this.orm.em.find(Promotion, {}, { fields: ['*', 'users'] })).map(
(p) => p.toObject() as unknown as OutputPromotionDTO,
);
}
@CreateRequestContext()
async findLatest(): Promise<OutputPromotionDTO> {
const promotion = (
await this.orm.em.find(
Promotion,
{},
{ orderBy: { number: 'DESC' }, limit: 1, fields: ['*', 'picture', 'users'] },
)
)[0];
return promotion.toObject() as unknown as OutputPromotionDTO;
}
@CreateRequestContext()
async findCurrent(): Promise<OutputPromotionDTO[]> {
return (
await this.orm.em.find(
Promotion,
{},
{ orderBy: { number: 'DESC' }, limit: 5, fields: ['*', 'picture', 'users'] },
)
).map((p) => p.toObject() as unknown as OutputPromotionDTO);
}
@CreateRequestContext()
async findOne(number: number): Promise<OutputPromotionDTO> {
const promotion = await this.orm.em.findOne(Promotion, { number }, { fields: ['*', 'picture', 'users'] });
if (!promotion) throw new i18nNotFoundException('validations.promotion.invalid.not_found', { number });
return promotion.toObject() as unknown as OutputPromotionDTO;
}
@CreateRequestContext()
async getUsers(number: number): Promise<OutputBaseUserDTO[]> {
const promotion = await this.orm.em.findOne(Promotion, { number }, { fields: ['users'] });
if (!promotion) throw new i18nNotFoundException('validations.promotion.invalid.not_found', { number });
const res: OutputBaseUserDTO[] = [];
for (const user of promotion.users.getItems()) {
res.push({
id: user.id,
updated: user.updated,
created: user.created,
first_name: user.first_name,
last_name: user.last_name,
nickname: user.nickname,
});
}
return res;
}
@CreateRequestContext()
async updateLogo(number: number, file: Express.Multer.File): Promise<OutputFileDTO> {
const promotion = await this.orm.em.findOne(Promotion, { number }, { populate: ['picture'] });
if (!promotion) throw new i18nNotFoundException('validations.promotion.invalid.not_found', { number });
const fileInfos = await this.imagesService.writeOnDisk(file.buffer, {
directory: join(env.PROMOTION_BASE_PATH, 'logo'),
filename: `promotion_${promotion.number}`,
aspect_ratio: '1:1',
});
if (promotion.picture) {
this.imagesService.deleteFromDisk(promotion.picture);
promotion.picture.filename = fileInfos.filename;
promotion.picture.mimetype = fileInfos.mimetype;
promotion.picture.path = fileInfos.filepath;
promotion.picture.size = fileInfos.size;
await this.orm.em.persistAndFlush(promotion.picture);
} else
promotion.picture = this.orm.em.create(PromotionPicture, {
filename: fileInfos.filename,
mimetype: fileInfos.mimetype,
path: fileInfos.filepath,
picture_promotion: promotion,
size: fileInfos.size,
});
await this.orm.em.persistAndFlush(promotion);
return promotion.picture.toObject() as unknown as OutputFileDTO;
}
@CreateRequestContext()
async getLogo(number: number): Promise<PromotionPicture> {
const promotion = await this.orm.em.findOne(Promotion, { number }, { populate: ['picture'] });
if (!promotion) throw new i18nNotFoundException('validations.promotion.invalid.not_found', { number });
if (!promotion.picture) throw new i18nNotFoundException('validations.promotion.invalid.no_logo', { number });
delete promotion.picture.picture_promotion; // avoid circular reference
return promotion.picture;
}
@CreateRequestContext()
async deleteLogo(number: number): Promise<OutputMessageDTO> {
const promotion = await this.orm.em.findOne(Promotion, { number }, { populate: ['picture'] });
if (!promotion) throw new i18nNotFoundException('validations.promotion.invalid.not_found', { number });
if (!promotion.picture) throw new i18nNotFoundException('validations.promotion.invalid.no_logo', { number });
this.imagesService.deleteFromDisk(promotion.picture);
await this.orm.em.removeAndFlush(promotion.picture);
return new OutputMessageDTO('validations.promotion.success.deleted_logo', { number: promotion.number });
}
}