/
subproject_assign.ts
107 lines (98 loc) · 3.66 KB
/
subproject_assign.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
import { VError } from "verror";
import { Ctx } from "lib/ctx";
import * as Result from "../../../result";
import { BusinessEvent } from "../business_event";
import { InvalidCommand } from "../errors/invalid_command";
import { NotAuthorized } from "../errors/not_authorized";
import { NotFound } from "../errors/not_found";
import { Identity } from "../organization/identity";
import { ServiceUser } from "../organization/service_user";
import * as UserRecord from "../organization/user_record";
import * as NotificationCreated from "./notification_created";
import * as Project from "./project";
import * as Subproject from "./subproject";
import * as SubprojectAssigned from "./subproject_assigned";
import * as SubprojectEventSourcing from "./subproject_eventsourcing";
import logger from "lib/logger";
interface Repository {
getSubproject(): Promise<Result.Type<Subproject.Subproject>>;
getUsersForIdentity(identity: Identity): Promise<Result.Type<UserRecord.Id[]>>;
}
export async function assignSubproject(
ctx: Ctx,
issuer: ServiceUser,
projectId: Project.Id,
subprojectId: Subproject.Id,
assignee: Identity,
repository: Repository,
): Promise<Result.Type<{ newEvents: BusinessEvent[]; subproject: Subproject.Subproject }>> {
let subproject = await repository.getSubproject();
if (Result.isErr(subproject)) {
return new NotFound(ctx, "subproject", subprojectId);
}
if (subproject.assignee === assignee) {
// This is already assigned to that user.
return { newEvents: [], subproject };
}
logger.trace({ issuer, assignee, projectId, subprojectId }, "Creating suproject_assign event");
const subprojectAssignedResult = SubprojectAssigned.createEvent(
ctx.source,
issuer.id,
projectId,
subprojectId,
assignee,
new Date().toISOString(),
issuer.metadata,
);
if (Result.isErr(subprojectAssignedResult)) {
return new VError(subprojectAssignedResult, "failed to create event");
}
const subprojectAssigned = subprojectAssignedResult;
logger.trace({ issuer }, "Checking if user has permissions");
if (issuer.id !== "root") {
const intent = "subproject.assign";
if (!Subproject.permits(subproject, issuer, [intent])) {
return new NotAuthorized({ ctx, userId: issuer.id, intent, target: subproject });
}
}
logger.trace({ event: subprojectAssigned }, "Checking event validity");
const result = SubprojectEventSourcing.newSubprojectFromEvent(
ctx,
subproject,
subprojectAssigned,
);
if (Result.isErr(result)) {
return new InvalidCommand(ctx, subprojectAssigned, [result]);
}
subproject = result;
logger.trace("Creating notifications");
const recipientsResult = await repository.getUsersForIdentity(assignee);
if (Result.isErr(recipientsResult)) {
return new VError(recipientsResult, `fetch users for ${assignee} failed`);
}
const notifications = recipientsResult.reduce((notifications, recipient) => {
// The issuer doesn't receive a notification:
if (recipient !== issuer.id) {
const notification = NotificationCreated.createEvent(
ctx.source,
issuer.id,
recipient,
subprojectAssigned,
projectId,
undefined,
undefined,
new Date().toISOString(),
issuer.metadata,
);
if (Result.isErr(notification)) {
return new VError(notification, "failed to create notification event");
}
notifications.push(notification);
}
return notifications;
}, [] as NotificationCreated.Event[]);
if (Result.isErr(notifications)) {
return new VError(notifications, "failed to create notification events");
}
return { newEvents: [subprojectAssigned, ...notifications], subproject };
}