-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser-report-pdf-service.ts
79 lines (65 loc) · 2.49 KB
/
user-report-pdf-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
/**
* 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 page of user-report-pdf-service.
*
* @module internal/pdf/user-report-pdf-service
*/
import { BuyerReport, ReportProductEntry, SalesReport } from '../../entity/report/report';
import {
FileResponse,
Product,
UserReportParameters,
UserReportParametersType,
UserRouteParams,
} from 'pdf-generator-client';
import { UnstoredPdfService } from './pdf-service';
import { entryToProduct, getPDFTotalsFromReport, userToIdentity } from '../../helpers/pdf';
import User from '../../entity/user/user';
import { EntityManager } from 'typeorm';
export default class UserReportPdfService<T extends SalesReport | BuyerReport> extends UnstoredPdfService<T, UserRouteParams> {
routeConstructor = UserRouteParams;
private readonly type: UserReportParametersType;
constructor(type: UserReportParametersType, manager?: EntityManager) {
super(manager);
this.type = type;
}
generator(routeParams: UserRouteParams): Promise<FileResponse> {
return this.client.generateUserReport(routeParams);
}
async getParameters(entity: T): Promise<UserReportParameters> {
const sales: Product[] = [];
if (!entity.data.products) throw new Error('No products found in report');
entity.data.products.forEach((s: ReportProductEntry) => sales.push(entryToProduct(s)));
const user = await this.manager.findOne(User, { where: { id: entity.forId } });
let data: any = {
account: userToIdentity(user),
startDate: entity.fromDate,
endDate: entity.tillDate,
entries: sales,
type: this.type,
total: getPDFTotalsFromReport(entity),
};
if (entity instanceof SalesReport) {
data.description = entity.description;
}
return new UserReportParameters(data);
}
}