/
workflowitem_close.ts
174 lines (158 loc) · 5.73 KB
/
workflowitem_close.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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 { NotFound } from "../errors/not_found";
import { PreconditionError } from "../errors/precondition_error";
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 Workflowitem from "./workflowitem";
import { Id } from "./workflowitem";
import * as WorkflowitemClosed from "./workflowitem_closed";
import * as WorkflowitemEventSourcing from "./workflowitem_eventsourcing";
import { sortWorkflowitems } from "./workflowitem_ordering";
import logger from "lib/logger";
interface Repository {
getWorkflowitems(
projectId: string,
subprojectId: string,
): Promise<Result.Type<Workflowitem.Workflowitem[]>>;
getUsersForIdentity(identity: Identity): Promise<Result.Type<UserRecord.Id[]>>;
getSubproject(
projectId: string,
subprojectId: string,
): Promise<Result.Type<Subproject.Subproject>>;
applyWorkflowitemType(
event: BusinessEvent,
workflowitem: Workflowitem.Workflowitem,
): Result.Type<BusinessEvent[]>;
}
export async function closeWorkflowitem(
ctx: Ctx,
issuer: ServiceUser,
projectId: Project.Id,
subprojectId: Subproject.Id,
workflowitemId: Id,
repository: Repository,
rejectReason?: string,
): Promise<Result.Type<BusinessEvent[]>> {
const workflowitemsResult = await repository.getWorkflowitems(projectId, subprojectId);
if (Result.isErr(workflowitemsResult)) {
return new VError(workflowitemsResult, "failed to get workflowitems");
}
const workflowitems = workflowitemsResult;
const subproject = await repository.getSubproject(projectId, subprojectId);
if (Result.isErr(subproject)) {
return new VError(`Couldn't get workflowitem ordering for ${subprojectId}`, subproject);
}
const { workflowitemOrdering } = subproject;
const sortedWorkflowitems = sortWorkflowitems(workflowitems, workflowitemOrdering);
const workflowitemToClose = sortedWorkflowitems.find((item) => item.id === workflowitemId);
if (workflowitemToClose === undefined) {
return new NotFound(ctx, "workflowitem", workflowitemId);
}
// Don't do anything in case the workflowitem is already closed:
if (workflowitemToClose.status === "closed") {
return [];
}
logger.trace(
{ closingUser: issuer, projectId, subprojectId, workflowitemId },
"Creating workflowitem_closed event",
);
const publisher = issuer.id;
const closeEvent = WorkflowitemClosed.createEvent(
ctx.source,
publisher,
projectId,
subprojectId,
workflowitemId,
new Date().toISOString(),
rejectReason,
issuer.metadata,
);
if (Result.isErr(closeEvent)) {
return new VError(closeEvent, "failed to create event");
}
const assignedIdentitiesResult = await repository.getUsersForIdentity(
workflowitemToClose.assignee,
);
if (Result.isErr(assignedIdentitiesResult)) {
return new VError(
assignedIdentitiesResult,
`fetch users for ${workflowitemToClose.assignee} failed`,
);
}
const assignedIdentities = assignedIdentitiesResult;
logger.trace({ closingUser: issuer }, "Checking user authorizaion");
if (issuer.id !== "root") {
if (subproject.validator !== undefined && subproject.validator !== issuer.id) {
return new PreconditionError(
ctx,
closeEvent,
"Only the validator of this subproject is allowed to close workflowitems",
);
} else if (!assignedIdentities.includes(issuer.id)) {
return new PreconditionError(
ctx,
closeEvent,
"Only the assignee is allowed to close the workflowitem.",
);
}
}
logger.trace("Making sure all previous workflowitems were already closed");
for (const item of sortedWorkflowitems) {
if (item.id === workflowitemId) {
break;
}
if (item.status !== "closed") {
return new PreconditionError(ctx, closeEvent, "all previous workflowitems must be closed");
}
}
logger.trace({ event: closeEvent }, "Checking event validity");
const result = WorkflowitemEventSourcing.newWorkflowitemFromEvent(
ctx,
workflowitemToClose,
closeEvent,
);
if (Result.isErr(result)) {
return new InvalidCommand(ctx, closeEvent, [result]);
}
logger.trace("Creating notification events");
const notifications: Result.Type<NotificationCreated.Event[]> = assignedIdentities.reduce(
(notifications, recipient) => {
// The issuer doesn't receive a notification:
if (recipient !== issuer.id) {
const notification = NotificationCreated.createEvent(
ctx.source,
issuer.id,
recipient,
closeEvent,
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");
}
const workflowitemTypeEvents = repository.applyWorkflowitemType(closeEvent, workflowitemToClose);
if (Result.isErr(workflowitemTypeEvents)) {
return new VError(workflowitemTypeEvents, "failed to apply workflowitem type");
}
return [closeEvent, ...notifications, ...workflowitemTypeEvents];
}