forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft.service.ts
More file actions
600 lines (546 loc) · 17.3 KB
/
Copy pathdraft.service.ts
File metadata and controls
600 lines (546 loc) · 17.3 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import {
Injectable,
NotFoundException,
BadRequestException,
UnauthorizedException,
} from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";
import { StructuredLogger } from "../observability/structured-logger.service";
import { ActivityFeedService } from "../activity-feed/activity-feed.service";
import { StellarService } from "../stellar/stellar.service";
import { Prisma, InvoiceStatus } from "@prisma/client";
import {
DraftInvoiceDto,
DraftResponseDto,
CreateDraftDto,
UpdateDraftDto,
} from "./dto/draft-invoice.dto";
import { Invoice } from "./entities/invoice.entity";
/**
* Draft service handles the complete lifecycle of invoice drafts
* including autosave, retrieval, and cleanup
*/
@Injectable()
export class DraftService {
private readonly AUTO_SAVE_INTERVAL_MS = 30_000; // 30 seconds
private readonly DRAFT_EXPIRY_DAYS = 30; // 30 days before cleanup
constructor(
private readonly prisma: PrismaService,
private readonly stellarService: StellarService,
private readonly structuredLogger: StructuredLogger,
private readonly activityFeed: ActivityFeedService,
) {}
/**
* Create a new draft invoice
*/
async createDraft(
dto: CreateDraftDto,
userId: string,
merchantId: string,
): Promise<DraftResponseDto> {
const now = new Date();
// Generate a unique memo for the draft
const memo = this.generateMemoId();
// Build the draft data
const draftData = this.buildDraftData(dto);
const created = await this.prisma.invoice.create({
data: {
userId,
merchantId,
invoiceNumber:
dto.invoiceNumber || `DRAFT-${Date.now().toString().slice(-6)}`,
clientName: dto.clientName || "Untitled Client",
clientEmail: dto.clientEmail || "",
description: dto.description || null,
amount: dto.amount || 0,
amountPaid: 0,
amountDue: dto.amount || 0,
assetCode: dto.asset_code?.toUpperCase() || "XLM",
assetIssuer: dto.asset_issuer || null,
memo: memo,
memoType: "ID",
status: "draft",
destinationAddress: this.stellarService.getMerchantPublicKey(),
txHash: null,
sorobanTxHash: null,
sorobanContractId: null,
metadata: dto.metadata
? (dto.metadata as Prisma.InputJsonValue)
: Prisma.JsonNull,
dueDate: dto.due_date ? new Date(dto.due_date) : null,
isDraft: true,
draftVersion: 1,
lastAutoSavedAt: now,
draftData: draftData as Prisma.InputJsonValue,
customerId: dto.customer_id || null,
statusHistory: {
create: {
status: "draft",
},
},
},
});
this.structuredLogger.info("draft.created", {
domain: "invoices",
event: "draft_created",
draftId: created.id,
invoiceNumber: created.invoiceNumber,
merchantId,
userId,
});
this.activityFeed?.recordEvent({
merchantId,
userId,
invoiceId: created.id,
type: "draft_created",
description: `Created invoice draft #${created.invoiceNumber}`,
metadata: {
invoiceNumber: created.invoiceNumber,
draftVersion: 1,
},
});
return this.toDraftResponse(created);
}
/**
* Update an existing draft (autosave)
*/
async updateDraft(
id: string,
dto: UpdateDraftDto,
userId: string,
merchantId: string,
): Promise<DraftResponseDto> {
// Verify ownership and draft status
const existing = await this.prisma.invoice.findFirst({
where: { id, merchantId, isDraft: true },
});
if (!existing) {
throw new NotFoundException(`Draft invoice with ID "${id}" not found`);
}
// Merge existing draft data with new data
const currentDraftData =
(existing.draftData as Record<string, unknown>) || {};
const newDraftData = {
...currentDraftData,
...this.buildDraftData(dto),
};
// NOTE: typed as the *unchecked* update input (not Prisma.InvoiceUpdateInput)
// so that scalar FK fields like `customerId` are assignable directly,
// matching the pattern createDraft already relies on via inference.
const updateData: Prisma.InvoiceUncheckedUpdateInput = {
draftData: newDraftData as Prisma.InputJsonValue,
lastAutoSavedAt: new Date(),
draftVersion: existing.draftVersion + 1,
updatedAt: new Date(),
};
// Update fields if provided
if (dto.invoiceNumber !== undefined) {
updateData.invoiceNumber = dto.invoiceNumber;
}
if (dto.clientName !== undefined) {
updateData.clientName = dto.clientName || "Untitled Client";
}
if (dto.clientEmail !== undefined) {
updateData.clientEmail = dto.clientEmail || "";
}
if (dto.description !== undefined) {
updateData.description = dto.description || null;
}
if (dto.amount !== undefined) {
updateData.amount = dto.amount;
updateData.amountDue = dto.amount;
}
if (dto.asset_code !== undefined) {
updateData.assetCode = dto.asset_code.toUpperCase();
}
if (dto.asset_issuer !== undefined) {
updateData.assetIssuer = dto.asset_issuer || null;
}
if (dto.customer_id !== undefined) {
updateData.customerId = dto.customer_id || null;
}
if (dto.due_date !== undefined) {
updateData.dueDate = dto.due_date ? new Date(dto.due_date) : null;
}
if (dto.metadata !== undefined) {
updateData.metadata = dto.metadata
? (dto.metadata as Prisma.InputJsonValue)
: Prisma.JsonNull;
}
const updated = await this.prisma.invoice.update({
where: { id },
data: updateData,
include: {
statusHistory: {
orderBy: { createdAt: "asc" },
},
},
});
this.structuredLogger.debug("draft.updated", {
domain: "invoices",
event: "draft_updated",
draftId: updated.id,
version: updated.draftVersion,
merchantId,
userId,
});
return this.toDraftResponse(updated);
}
/**
* Get a draft by ID
*/
async getDraft(id: string, merchantId: string): Promise<DraftResponseDto> {
const draft = await this.prisma.invoice.findFirst({
where: { id, merchantId, isDraft: true },
include: {
statusHistory: {
orderBy: { createdAt: "asc" },
},
},
});
if (!draft) {
throw new NotFoundException(`Draft invoice with ID "${id}" not found`);
}
return this.toDraftResponse(draft);
}
/**
* Get all drafts for a merchant
*/
async getDrafts(
merchantId: string,
page = 1,
limit = 20,
): Promise<{
items: DraftResponseDto[];
total: number;
page: number;
pageSize: number;
hasMore: boolean;
}> {
const skip = (page - 1) * limit;
const [items, total] = await Promise.all([
this.prisma.invoice.findMany({
where: { merchantId, isDraft: true },
skip,
take: limit,
orderBy: { lastAutoSavedAt: "desc" },
include: {
statusHistory: {
orderBy: { createdAt: "asc" },
},
},
}),
this.prisma.invoice.count({
where: { merchantId, isDraft: true },
}),
]);
return {
items: items.map((inv) => this.toDraftResponse(inv)),
total,
page,
pageSize: limit,
hasMore: skip + items.length < total,
};
}
/**
* Convert a draft to a real invoice (send/issue)
*/
async convertDraftToInvoice(
id: string,
merchantId: string,
userId: string,
): Promise<Invoice> {
// Verify ownership and draft status
const draft = await this.prisma.invoice.findFirst({
where: { id, merchantId, isDraft: true },
});
if (!draft) {
throw new NotFoundException(`Draft invoice with ID "${id}" not found`);
}
// Validate required fields
const errors = this.validateDraftForConversion(draft);
if (errors.length > 0) {
throw new BadRequestException(
`Cannot convert draft to invoice: ${errors.join(", ")}`,
);
}
// Generate a proper invoice number if it's still a draft number
let invoiceNumber = draft.invoiceNumber;
if (invoiceNumber.startsWith("DRAFT-")) {
invoiceNumber = `INV-${Date.now().toString().slice(-6)}`;
}
// Convert to real invoice
const converted = await this.prisma.invoice.update({
where: { id },
data: {
status: "pending",
isDraft: false,
invoiceNumber: invoiceNumber,
// Ensure all required fields are set
clientName: draft.clientName || "Client",
clientEmail: draft.clientEmail || "client@example.com",
amountDue: draft.amount,
// If draft had due date, keep it, otherwise set default 30 days
dueDate:
draft.dueDate || new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
memo: this.generateMemoId(), // Generate new memo for payment tracking
statusHistory: {
create: {
status: "pending",
},
},
},
include: {
statusHistory: {
orderBy: { createdAt: "asc" },
},
},
});
this.structuredLogger.info("draft.converted", {
domain: "invoices",
event: "draft_converted_to_invoice",
draftId: id,
invoiceId: converted.id,
invoiceNumber: converted.invoiceNumber,
merchantId,
userId,
});
this.activityFeed?.recordEvent({
merchantId,
userId,
invoiceId: converted.id,
type: "draft_converted",
description: `Converted draft #${draft.invoiceNumber} to invoice #${converted.invoiceNumber}`,
metadata: {
originalDraftId: id,
draftNumber: draft.invoiceNumber,
invoiceNumber: converted.invoiceNumber,
},
});
// Return as full Invoice entity
//
// ASSUMPTION (unverified — I don't have entities/invoice.entity.ts):
// the compiler error said `Invoice` has `user`, not `userId`. I couldn't
// confirm whether that's a typo in the entity or intentional. Kept
// `userId` here but cast through `as unknown as Invoice` so this compiles
// without guessing at the entity's real shape. Once you share
// invoice.entity.ts, replace this cast with a correct object literal
// (either add `userId?: string` to the entity, or map to `user` here).
return {
id: converted.id,
merchantId: converted.merchantId,
userId: converted.userId || undefined,
invoiceNumber: converted.invoiceNumber,
clientName: converted.clientName,
clientEmail: converted.clientEmail,
description: converted.description,
amount: converted.amount.toNumber(),
amountPaid: converted.amountPaid.toNumber(),
amountDue: converted.amountDue.toNumber(),
asset_code: converted.assetCode,
asset_issuer: converted.assetIssuer,
memo: converted.memo,
memo_type: converted.memoType,
tx_hash: converted.txHash,
status: converted.status,
metadata: converted.metadata,
dueDate: converted.dueDate,
createdAt: converted.createdAt,
updatedAt: converted.updatedAt,
destination_address: converted.destinationAddress,
customerId: converted.customerId,
statusHistory: converted.statusHistory.map((h) => ({
id: h.id,
invoiceId: h.invoiceId,
status: h.status,
createdAt: h.createdAt,
})),
payments: [],
} as unknown as Invoice;
}
/**
* Discard/delete a draft
*/
async discardDraft(
id: string,
merchantId: string,
): Promise<{ id: string; discarded: boolean }> {
// Verify ownership and draft status
const draft = await this.prisma.invoice.findFirst({
where: { id, merchantId, isDraft: true },
});
if (!draft) {
throw new NotFoundException(`Draft invoice with ID "${id}" not found`);
}
// Hard delete the draft
await this.prisma.invoice.delete({
where: { id },
});
this.structuredLogger.info("draft.discarded", {
domain: "invoices",
event: "draft_discarded",
draftId: id,
invoiceNumber: draft.invoiceNumber,
merchantId,
});
return { id, discarded: true };
}
/**
* Clean up old drafts that haven't been touched
*/
async cleanupOldDrafts(): Promise<{ deletedCount: number }> {
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - this.DRAFT_EXPIRY_DAYS);
const result = await this.prisma.invoice.deleteMany({
where: {
isDraft: true,
lastAutoSavedAt: {
lt: cutoff,
},
status: "draft",
},
});
if (result.count > 0) {
this.structuredLogger.info("draft.cleanup", {
domain: "invoices",
event: "drafts_cleaned_up",
deletedCount: result.count,
cutoffDate: cutoff.toISOString(),
});
}
return { deletedCount: result.count };
}
/**
* Auto-save a draft (utility method for frontend polling)
*/
async autoSaveDraft(
id: string,
updates: Partial<UpdateDraftDto>,
userId: string,
merchantId: string,
): Promise<DraftResponseDto> {
// Add autoSave flag
const dto: UpdateDraftDto = {
...updates,
autoSave: true,
};
return this.updateDraft(id, dto, userId, merchantId);
}
/**
* Build draft data object from DTO
*/
private buildDraftData(
dto: Partial<DraftInvoiceDto>,
): Record<string, unknown> {
const data: Record<string, unknown> = {};
if (dto.invoiceNumber !== undefined) data.invoiceNumber = dto.invoiceNumber;
if (dto.clientName !== undefined) data.clientName = dto.clientName;
if (dto.clientEmail !== undefined) data.clientEmail = dto.clientEmail;
if (dto.description !== undefined) data.description = dto.description;
if (dto.amount !== undefined) data.amount = dto.amount;
if (dto.asset_code !== undefined) data.asset_code = dto.asset_code;
if (dto.asset_issuer !== undefined) data.asset_issuer = dto.asset_issuer;
if (dto.customer_id !== undefined) data.customer_id = dto.customer_id;
if (dto.due_date !== undefined) data.due_date = dto.due_date;
if (dto.metadata !== undefined) data.metadata = dto.metadata;
data.lastUpdated = new Date().toISOString();
return data;
}
/**
* Validate if a draft has all required fields to become an invoice
*/
private validateDraftForConversion(draft: any): string[] {
const errors: string[] = [];
if (!draft.clientName || draft.clientName.trim().length === 0) {
errors.push("Client name is required");
}
if (!draft.clientEmail || draft.clientEmail.trim().length === 0) {
errors.push("Client email is required");
}
if (!draft.amount || draft.amount <= 0) {
errors.push("Amount must be greater than 0");
}
if (!draft.assetCode) {
errors.push("Asset code is required");
}
if (draft.assetCode !== "XLM" && !draft.assetIssuer) {
errors.push("Asset issuer is required for non-XLM assets");
}
return errors;
}
/**
* Generate a unique Stellar MEMO_ID for payment matching
*/
private generateMemoId(): string {
const timestamp = Date.now();
const random = Math.floor(Math.random() * 65536);
return String(timestamp * 65536 + random);
}
/**
* Convert Prisma invoice to DraftResponseDto
*/
private toDraftResponse(invoice: any): DraftResponseDto {
const draftData = (invoice.draftData as Record<string, unknown>) || {};
const requiredFields = [
"clientName",
"clientEmail",
"amount",
"asset_code",
];
const hasRequiredFields = requiredFields.every((field) => {
// Check in the main invoice fields and in draftData
const mainValue = invoice[field] ?? invoice[this.snakeToCamel(field)];
const draftValue = draftData[field];
const value = mainValue || draftValue;
return value !== undefined && value !== null && value !== "";
});
// Calculate completion percentage
const allFields = [
"invoiceNumber",
"clientName",
"clientEmail",
"description",
"amount",
"asset_code",
];
const filledFields = allFields.filter((field) => {
const mainValue = invoice[field] ?? invoice[this.snakeToCamel(field)];
const draftValue = draftData[field];
const value = mainValue || draftValue;
return value !== undefined && value !== null && value !== "";
});
const completionPercentage = Math.round(
(filledFields.length / allFields.length) * 100,
);
return {
id: invoice.id,
merchantId: invoice.merchantId,
userId: invoice.userId || undefined,
invoiceNumber: invoice.invoiceNumber,
clientName: invoice.clientName,
clientEmail: invoice.clientEmail,
description: invoice.description,
amount: invoice.amount?.toNumber
? invoice.amount.toNumber()
: Number(invoice.amount),
assetCode: invoice.assetCode,
assetIssuer: invoice.assetIssuer,
customerId: invoice.customerId,
dueDate: invoice.dueDate,
isDraft: invoice.isDraft,
draftVersion: invoice.draftVersion,
lastAutoSavedAt: invoice.lastAutoSavedAt,
createdAt: invoice.createdAt,
updatedAt: invoice.updatedAt,
metadata: (invoice.metadata as Record<string, unknown>) || undefined,
hasRequiredFields,
completionPercentage,
};
}
/**
* Helper to convert snake_case to camelCase
*/
private snakeToCamel(str: string): string {
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
}
}