Repository navigation
Expand file tree
/
Copy pathcontracts.service.ts
More file actions
418 lines (388 loc) · 13.8 KB
/
Copy pathcontracts.service.ts
File metadata and controls
418 lines (388 loc) · 13.8 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import {
CreateContractDto,
UpdateContractDto,
} from "../modules/contracts/dto/contract.dto";
import { Contract } from "../db/types";
import type { IContractRepository } from "../repositories/contractRepository";
import { SorobanService } from "./soroban.service";
import type {
CursorPaginationInput,
CursorPage,
} from "../contracts/cursor.types";
import { ContractCacheService } from "./contractCache.service";
import {
MAX_MILESTONES_PER_CONTRACT,
MAX_CONTRACT_AMOUNT_STROOPS,
validateContractBounds,
ContractBoundsError,
} from "../contracts/bounds";
import {
NotFoundError,
MissingVersionError,
InvalidVersionError,
VersionConflictError,
} from "../errors/appError";
import { parseBoolEnv } from "../config/env";
import { parseRetentionDays } from "../utils/softDelete";
import { createLogger } from "../logger";
import { EventIngestionService } from "../events/eventIngestionService";
const log = createLogger({ service: "contracts" });
/** Env key for contracts soft-delete retention window (days). */
export const CONTRACTS_SOFT_DELETE_RETENTION_DAYS_ENV =
"CONTRACTS_SOFT_DELETE_RETENTION_DAYS";
/**
* @dev Service layer for managing Freelancer Escrow Contracts.
* Handles business logic, database interactions,
* and orchestration with the Soroban smart contract service.
*/
export class ContractsService {
private contractRepository: IContractRepository;
private sorobanService: SorobanService;
private cache?: ContractCacheService;
/**
* When `false`, milestone fields are stripped from create/update payloads
* before any validation or persistence occurs. The feature is fully
* transparent to callers — requests still succeed but milestones are
* silently ignored.
*
* Defaults to `true` (read from `MILESTONES_ENABLED` env var at
* construction time) so the flag can be injected in tests without
* touching `process.env`.
*/
private readonly milestonesEnabled: boolean;
constructor(
contractRepository: IContractRepository,
milestonesEnabled?: boolean,
) {
this.sorobanService = new SorobanService();
this.contractRepository = contractRepository;
this.milestonesEnabled =
milestonesEnabled !== undefined
? milestonesEnabled
: parseBoolEnv("MILESTONES_ENABLED", true);
}
/**
* Retention window in days for contract soft deletion.
*/
public getRetentionDays(): number {
return parseRetentionDays(
process.env[CONTRACTS_SOFT_DELETE_RETENTION_DAYS_ENV],
);
}
/**
* Retrieves all contracts from the repository.
* @returns Array of contract metadata including version field.
*/
public async getAllContracts(options?: {
includeDeleted?: boolean;
}): Promise<Contract[]> {
if (this.cache) {
return this.cache.getAllContracts(() =>
this.contractRepository.findAll(options),
);
}
return this.contractRepository.findAll(options);
}
/**
* Retrieves a single contract by ID.
* @param id The contract UUID.
* @returns The contract or undefined if not found.
*/
public async getContractById(
id: string,
options?: { includeDeleted?: boolean },
): Promise<Contract | undefined> {
if (this.cache) {
return this.cache.getContractById(id, () =>
this.contractRepository.findById(id, options),
);
}
return this.contractRepository.findById(id, options);
}
/**
* Returns a cursor-paginated page of contracts ordered by `createdAt DESC`.
*
* @param input - Optional `limit` (1–100) and opaque `cursor` string.
* @returns A {@link CursorPage} with items and next-page cursor.
*/
public async getContractsPage(
input: CursorPaginationInput & { includeDeleted?: boolean } = {},
): Promise<CursorPage<Contract>> {
if (this.cache) {
return this.cache.getContractsPage(input, () =>
this.contractRepository.findPage(input),
);
}
return this.contractRepository.findPage(input);
}
/**
* Creates a new contract off-chain, preparing it for escrow deposit.
* Enforces milestone count and total amount caps before persisting.
* @param data The contract details conforming to CreateContractDto.
* @param correlationId Optional correlation ID for distributed tracing.
* @returns The newly created contract object.
* @throws ContractBoundsError if budget or milestone totals exceed policy limits.
*/
public async createContract(
data: CreateContractDto,
correlationId?: string,
): Promise<Contract> {
const traceCtx = correlationId ? { correlationId } : {};
// Strip milestones when the feature flag is disabled. Validation and
// budget-cap checks are skipped entirely — the contract is created as if
// no milestones were supplied.
const effectiveData: CreateContractDto = this.milestonesEnabled
? data
: { ...data, milestones: undefined };
const boundsCheck = validateContractBounds(
effectiveData.budget,
effectiveData.milestones,
);
if (!boundsCheck.valid) {
throw new ContractBoundsError((boundsCheck as any).error);
}
// Enforce that the sum of milestone amounts does not exceed the contract
// budget. `validateContractBounds` only guards the absolute policy cap
// (MAX_CONTRACT_AMOUNT_STROOPS); the per-contract budget is the tighter,
// caller-supplied limit that milestone payouts must never overrun.
if (effectiveData.milestones && effectiveData.milestones.length > 0) {
const totalMilestoneAmount = effectiveData.milestones.reduce(
(sum, milestone) => sum + milestone.amount,
0,
);
if (totalMilestoneAmount > effectiveData.budget) {
throw new ContractBoundsError(
`Total milestone amount exceeds maximum contract amount ` +
`(milestones total ${totalMilestoneAmount} exceeds budget of ${effectiveData.budget})`,
);
}
}
const newContract = await this.contractRepository.create({
title: effectiveData.title,
clientId: effectiveData.clientId,
freelancerId: effectiveData.freelancerId ?? "",
amount: effectiveData.budget,
status: effectiveData.status || "draft",
});
log.info("ContractsService.createContract: contract created", {
...traceCtx,
contractId: newContract.id,
});
// Notify the Soroban service to prepare the transaction
try {
await this.sorobanService.prepareEscrow(newContract.id, data.budget);
log.info("ContractsService.createContract: soroban escrow prepared", {
...traceCtx,
contractId: newContract.id,
});
} catch (error) {
log.warn(
"ContractsService.createContract: soroban prepareEscrow failed",
{
...traceCtx,
contractId: newContract.id,
err: error as Error,
},
);
}
this.cache?.invalidateLists();
return newContract;
}
/**
* Updates a contract using Optimistic Concurrency Control (OCC).
*
* Requires the caller to supply the `version` they last observed. The update
* succeeds only when the stored version matches the supplied value; the version
* is then atomically incremented by 1. If the stored version differs (another
* writer got there first), a {@link VersionConflictError} is thrown.
*
* Maps every updatable field from {@link UpdateContractDto} into the update
* payload and re-runs {@link validateContractBounds} whenever `budget` or
* `milestones` are included in the patch. Rejects empty patches with a
* validation error so callers receive a clear signal rather than a misleading
* 200 that changed nothing.
*
* @param id - UUID of the contract to update.
* @param dto - Partial update payload including the OCC `version`.
* @param correlationId - Optional correlation ID for distributed tracing.
* @returns The updated Contract with an incremented version.
* @throws {MissingVersionError} When `version` is not provided.
* @throws {InvalidVersionError} When `version` is not a non-negative integer.
* @throws {ContractBoundsError} When amount or milestone bounds are violated.
* @throws {VersionConflictError} When the version is stale (another update won).
* @throws {NotFoundError} When the contract ID does not exist.
*
* @security The version check is enforced at the database level via a single
* atomic `UPDATE ... WHERE version = ?` statement. It cannot be bypassed by
* omitting the version field because this method validates it before calling
* the repository.
*/
public async updateContract(
id: string,
dto: UpdateContractDto,
correlationId?: string,
): Promise<Contract> {
const traceCtx = correlationId ? { correlationId } : {};
const { version, ...fields } = dto;
// Defense-in-depth: validate version even though middleware already checked
if (version === undefined || version === null) {
throw new MissingVersionError();
}
if (!Number.isInteger(version) || version < 0) {
throw new InvalidVersionError();
}
// Strip milestones from the update payload when the feature flag is
// disabled, so milestone data supplied by callers is silently ignored.
const effectiveFields = this.milestonesEnabled
? fields
: { ...fields, milestones: undefined };
// Reject no-op updates
const hasFields = Object.keys(effectiveFields).some(
(k) => (effectiveFields as Record<string, unknown>)[k] !== undefined,
);
if (!hasFields) {
throw new Error("At least one field must be provided for an update.");
}
// Re-validate bounds when amount or milestones are being changed
const budget = effectiveFields.budget;
const milestones = effectiveFields.milestones;
if (budget !== undefined || milestones !== undefined) {
// Fall back to 0 if budget is absent so the bounds check can still run on milestones alone
const boundsCheck = validateContractBounds(budget ?? 0, milestones);
if (!boundsCheck.valid) {
throw new ContractBoundsError((boundsCheck as any).error);
}
}
const updateFields: Partial<Contract> = {};
if (effectiveFields.title !== undefined)
updateFields.title = effectiveFields.title;
if (effectiveFields.status !== undefined)
updateFields.status = effectiveFields.status;
if (effectiveFields.budget !== undefined)
updateFields.amount = effectiveFields.budget;
if (effectiveFields.freelancerId !== undefined)
updateFields.freelancerId = effectiveFields.freelancerId ?? "";
const updated = await this.contractRepository.updateWithVersion(
id,
updateFields,
version,
);
log.info("ContractsService.updateContract: contract updated", {
...traceCtx,
contractId: id,
version,
});
return updated;
}
/**
* Soft-deletes a contract by ID.
* @param correlationId - Optional correlation ID for distributed tracing.
* @param now - Optional timestamp when soft-deletion occurs.
*/
public async deleteContract(
id: string,
correlationId?: string,
now?: Date,
): Promise<void> {
const traceCtx = correlationId ? { correlationId } : {};
const deleted = await this.contractRepository.delete(id, now);
if (!deleted) {
throw new NotFoundError(`Contract with id ${id} not found`);
}
this.cache?.invalidateLists();
log.info("ContractsService.deleteContract: contract soft-deleted", {
...traceCtx,
contractId: id,
});
}
/**
* Restores a soft-deleted contract within the retention window.
* @param id - UUID of the contract to restore.
* @param correlationId - Optional correlation ID for distributed tracing.
* @param now - Optional reference timestamp for retention window evaluation.
*/
public async restoreContract(
id: string,
correlationId?: string,
now?: Date,
): Promise<Contract> {
const traceCtx = correlationId ? { correlationId } : {};
const retentionDays = this.getRetentionDays();
const restored = await this.contractRepository.restore(
id,
now,
retentionDays,
);
this.cache?.invalidateLists();
log.info("ContractsService.restoreContract: contract restored", {
...traceCtx,
contractId: id,
});
return restored;
}
/**
* Hard-deletes contracts whose soft-deletion timestamp exceeds the retention window.
* @param now - Optional reference timestamp for cutoff evaluation.
*/
public async purgeExpiredContracts(now?: Date): Promise<number> {
const retentionDays = this.getRetentionDays();
const count = await this.contractRepository.purgeExpired(
now,
retentionDays,
);
if (count > 0) {
this.cache?.invalidateLists();
}
return count;
}
/**
* Retrieves contract statistics.
*/
public async getContractStats() {
if (this.cache) {
return this.cache.getContractStats(async () => {
const all = await this.contractRepository.findAll();
return {
total: all.length,
totalBudget: all.reduce((sum, c) => sum + c.amount, 0),
byStatus: all.reduce(
(acc, c) => {
acc[c.status] = (acc[c.status] || 0) + 1;
return acc;
},
{} as Record<string, number>,
),
};
});
}
const all = await this.contractRepository.findAll();
const stats = {
total: all.length,
totalBudget: all.reduce((sum, c) => sum + c.amount, 0),
byStatus: all.reduce(
(acc, c) => {
acc[c.status] = (acc[c.status] || 0) + 1;
return acc;
},
{} as Record<string, number>,
),
};
return stats;
}
/**
* Retrieves policy bounds.
*/
public getBounds() {
return {
maxMilestones: MAX_MILESTONES_PER_CONTRACT,
maxAmount: MAX_CONTRACT_AMOUNT_STROOPS,
};
}
/**
* Retrieves contract event history by contract ID.
* @param id - UUID of the contract.
*/
public async getContractHistory(_id: string) {
return [];
}
}