-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTargetUnitRepository.ts
More file actions
53 lines (46 loc) · 1.47 KB
/
Copy pathTargetUnitRepository.ts
File metadata and controls
53 lines (46 loc) · 1.47 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
import { Pool } from 'mysql2/promise';
import { TargetUnitRepositoryError } from '../../common/errors';
import { TargetUnit } from '../../common/types';
import { ITargetUnitRepository } from './ITargetUnitRepository';
import { TARGET_UNITS_QUERY } from './queries';
import { TargetUnitRow } from './types';
export class TargetUnitRepository implements ITargetUnitRepository {
private readonly pool: Pool;
constructor(pool: Pool) {
this.pool = pool;
}
private static mapRowToTargetUnit = ({
group_id,
group_name,
project_id,
project_name,
user_id,
username,
spent_on,
project_hours,
total_hours,
}: TargetUnitRow): TargetUnit => ({
group_id: Number(group_id),
group_name: String(group_name),
project_id: Number(project_id),
project_name: String(project_name),
user_id: Number(user_id),
username: String(username),
spent_on: String(spent_on),
project_hours: Number(project_hours),
total_hours: Number(total_hours),
});
async getTargetUnits(): Promise<TargetUnit[]> {
try {
const [rows] = await this.pool.query<TargetUnitRow[]>(TARGET_UNITS_QUERY);
if (!Array.isArray(rows)) {
throw new TargetUnitRepositoryError('Query did not return an array');
}
return rows.map(TargetUnitRepository.mapRowToTargetUnit);
} catch (error) {
throw new TargetUnitRepositoryError(
`TargetUnitRepository.getTargetUnits failed: ${(error as Error).message}`,
);
}
}
}