-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathlearner-participation-controller.js
73 lines (61 loc) · 2.64 KB
/
learner-participation-controller.js
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
import { DomainTransaction } from '../../../shared/domain/DomainTransaction.js';
import { monitoringTools } from '../../../shared/infrastructure/monitoring-tools.js';
import * as requestResponseUtils from '../../../shared/infrastructure/utils/request-response-utils.js';
import { usecases } from '../domain/usecases/index.js';
import * as campaignParticipationSerializer from '../infrastructure/serializers/jsonapi/campaign-participation-serializer.js';
import * as sharedProfileForCampaignSerializer from '../infrastructure/serializers/jsonapi/shared-profile-for-campaign-serializer.js';
const save = async function (request, h, dependencies = { campaignParticipationSerializer, monitoringTools }) {
const userId = request.auth.credentials.userId;
const campaignParticipation = await dependencies.campaignParticipationSerializer.deserialize(request.payload);
const { campaignParticipation: campaignParticipationCreated } = await DomainTransaction.execute(() => {
return usecases.startCampaignParticipation({ campaignParticipation, userId });
});
return h.response(dependencies.campaignParticipationSerializer.serialize(campaignParticipationCreated)).created();
};
const shareCampaignResult = async function (request, _) {
const userId = request.auth.credentials.userId;
const campaignParticipationId = request.params.campaignParticipationId;
return DomainTransaction.execute(async () => {
await usecases.shareCampaignResult({
userId,
campaignParticipationId,
});
return null;
});
};
const beginImprovement = async function (request) {
const userId = request.auth.credentials.userId;
const campaignParticipationId = request.params.campaignParticipationId;
return DomainTransaction.execute(async () => {
await usecases.beginCampaignParticipationImprovement({
campaignParticipationId,
userId,
});
return null;
});
};
const getSharedCampaignParticipationProfile = async function (
request,
h,
dependencies = {
sharedProfileForCampaignSerializer,
requestResponseUtils,
},
) {
const authenticatedUserId = request.auth.credentials.userId;
const campaignId = request.params.campaignId;
const locale = dependencies.requestResponseUtils.extractLocaleFromRequest(request);
const sharedProfileForCampaign = await usecases.getSharedCampaignParticipationProfile({
userId: authenticatedUserId,
campaignId,
locale,
});
return dependencies.sharedProfileForCampaignSerializer.serialize(sharedProfileForCampaign);
};
const learnerParticipationController = {
save,
shareCampaignResult,
beginImprovement,
getSharedCampaignParticipationProfile,
};
export { learnerParticipationController };