forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise.ts
111 lines (107 loc) · 3.33 KB
/
exercise.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
import type {
ExerciseIds,
ExerciseTimeline,
StateExport,
} from 'digital-fuesim-manv-shared';
import { ExerciseState } from 'digital-fuesim-manv-shared';
import { isEmpty } from 'lodash-es';
import { importExercise } from '../../../utils/import-exercise';
import type { DatabaseService } from '../../../database/services/database-service';
import { UserReadableIdGenerator } from '../../../utils/user-readable-id-generator';
import { exerciseMap } from '../../exercise-map';
import { ExerciseWrapper } from '../../exercise-wrapper';
import type { HttpResponse } from '../utils';
export async function postExercise(
databaseService: DatabaseService,
importObject: StateExport
): Promise<HttpResponse<ExerciseIds>> {
try {
const participantId = UserReadableIdGenerator.generateId();
const trainerId = UserReadableIdGenerator.generateId(8);
const newExerciseOrError = isEmpty(importObject)
? ExerciseWrapper.create(
participantId,
trainerId,
databaseService,
ExerciseState.create(participantId)
)
: await importExercise(
importObject,
{ participantId, trainerId },
databaseService
);
if (!(newExerciseOrError instanceof ExerciseWrapper)) {
return newExerciseOrError;
}
exerciseMap.set(participantId, newExerciseOrError);
exerciseMap.set(trainerId, newExerciseOrError);
return {
statusCode: 201,
body: {
participantId,
trainerId,
},
};
} catch (error: unknown) {
if (error instanceof RangeError) {
return {
statusCode: 503,
body: {
message: 'No ids available.',
},
};
}
throw error;
}
}
export function getExercise(exerciseId: string): HttpResponse {
const exerciseExists = exerciseMap.has(exerciseId);
return {
statusCode: exerciseExists ? 200 : 404,
body: undefined,
};
}
export async function deleteExercise(
exerciseId: string
): Promise<HttpResponse> {
const exerciseWrapper = exerciseMap.get(exerciseId);
if (exerciseWrapper === undefined) {
return {
statusCode: 404,
body: {
message: `Exercise with id '${exerciseId}' was not found`,
},
};
}
if (exerciseWrapper.getRoleFromUsedId(exerciseId) !== 'trainer') {
return {
statusCode: 403,
body: {
message:
'Exercises can only be deleted by using their trainer id',
},
};
}
await exerciseWrapper.deleteExercise();
return {
statusCode: 204,
body: undefined,
};
}
export async function getExerciseHistory(
exerciseId: string
): Promise<HttpResponse<ExerciseTimeline>> {
const exerciseWrapper = exerciseMap.get(exerciseId);
if (exerciseWrapper === undefined) {
return {
statusCode: 404,
body: {
message: `Exercise with id '${exerciseId}' was not found`,
},
};
}
return {
statusCode: 200,
body: await exerciseWrapper.getTimeLine(),
};
}