-
Notifications
You must be signed in to change notification settings - Fork 3
/
banner-service.ts
215 lines (189 loc) · 6.31 KB
/
banner-service.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of the banner-service.
*
* @module banners
*/
import { FindManyOptions } from 'typeorm';
import BannerRequest from '../controller/request/banner-request';
import { BannerResponse, PaginatedBannerResponse } from '../controller/response/banner-response';
import Banner from '../entity/banner';
import QueryFilter, { FilterMapping } from '../helpers/query-filter';
import FileService from './file-service';
import { PaginationParameters } from '../helpers/pagination';
export interface BannerFilterParameters {
bannerId?: number,
active?: boolean,
}
export default class BannerService {
/**
* Verifies whether the banner request translates to a valid banner object
* @param {BannerRequest.model} br - the banner request to verify
* @returns {boolean} - whether banner is ok or not
*/
// eslint-disable-next-line class-methods-use-this
public static verifyBanner(br: BannerRequest): boolean {
const sDate = Date.parse(br.startDate);
const eDate = Date.parse(br.endDate);
return br.name !== ''
// duration must be integer greater than 0
&& br.duration > 0
&& Number.isInteger(br.duration)
&& br.active !== null
&& !Number.isNaN(sDate)
&& !Number.isNaN(eDate)
// end date cannot be in the past
&& eDate > new Date().getTime()
// end date must be later than start date
&& eDate > sDate;
}
/**
* Creates a banner from a banner request
* @param {BannerRequest.model} bannerReq - banner request
* @returns {Banner.model} - a banner entity created with the banner request
*/
public static asBanner(bannerReq: BannerRequest): Banner {
if (!bannerReq) {
return undefined;
}
return {
...bannerReq,
startDate: new Date(bannerReq.startDate),
endDate: new Date(bannerReq.endDate),
} as Banner;
}
/**
* Creates a banner response from a banner
* @param {Banner.model} banner - banner
* @returns {BannerResponse.model} - a banner response created with the banner
*/
public static asBannerResponse(banner: Banner): BannerResponse {
if (!banner) {
return undefined;
}
let image;
if (!banner.image) {
image = null;
} else {
image = banner.image.downloadName;
}
return {
id: banner.id,
name: banner.name,
image,
duration: banner.duration,
active: banner.active,
createdAt: banner.createdAt.toISOString(),
updatedAt: banner.updatedAt.toISOString(),
startDate: banner.startDate.toISOString(),
endDate: banner.endDate.toISOString(),
};
}
/**
* Returns all banners with options.
* @param filters - The filtering parameters.
* @param pagination - The pagination options.
* @returns {Array.<BannerResponse>} - all banners
*/
public static async getBanners(
filters: BannerFilterParameters, pagination: PaginationParameters = {},
): Promise<PaginatedBannerResponse> {
const { take, skip } = pagination;
const mapping: FilterMapping = {
bannerId: 'id',
active: 'active',
};
const options: FindManyOptions = {
where: QueryFilter.createFilterWhereClause(mapping, filters),
relations: ['image'],
order: { id: 'DESC' },
};
const banners = await Banner.find({
...options,
take,
skip,
});
const records = banners.map((banner) => this.asBannerResponse(banner));
return {
_pagination: {
take,
skip,
count: await Banner.count(options),
},
records,
};
}
/**
* Saves a banner to the database.
* @param bannerReq
* @returns {BannerResponse.model} - saved banner
*/
public static async createBanner(bannerReq: BannerRequest): Promise<BannerResponse> {
// save and return banner
const banner = this.asBanner(bannerReq);
await Banner.save(banner);
return this.asBannerResponse(banner);
}
/**
* Updates and returns banner with given id.
* @param id - requested banner id
* @param bannerReq
* @returns {BannerResponse.model} - updated banner
*/
public static async updateBanner(id: number, bannerReq: BannerRequest): Promise<BannerResponse> {
// check if banner in database
const bannerFound = await Banner.findOne({ where: { id } });
// return undefined if banner not found or request is invalid
if (!bannerFound || !this.verifyBanner(bannerReq)) {
return undefined;
}
// patch banner if found
const banner = this.asBanner(bannerReq);
await Banner.update(id, banner);
return this.asBannerResponse(await Banner.findOne({ where: { id }, relations: ['image'] }));
}
/**
* Deletes the requested banner from the database
* @param id - requested banner id
* @param fileService
* @returns {BannerResponse.model} - deleted banner
*/
public static async deleteBanner(id: number, fileService: FileService): Promise<BannerResponse> {
// check if banner in database
const banner = await Banner.findOne({ where: { id }, relations: ['image'] });
// return undefined if not found
if (!banner) {
return undefined;
}
// Delete banner if found
const bannerImage = banner.image;
if (banner.image) {
// Set image temporarily to null, so we can remove the foreign key constraint
banner.image = null;
await banner.save();
await fileService.deleteEntityFile(bannerImage);
}
await Banner.delete(id);
// Restore the banner image so the response will be correct
banner.image = bannerImage;
return this.asBannerResponse(banner);
}
}