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
162 lines (147 loc) · 4.66 KB
/
Copy pathdraft-service.ts
File metadata and controls
162 lines (147 loc) · 4.66 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
import { apiClient } from "./api-client";
import type {
DraftInvoice,
CreateDraftDto,
UpdateDraftDto,
DraftListResponse,
DiscardDraftResponse,
} from "./types/draft.types";
/**
* DraftService handles all draft-related API operations
* Provides methods for creating, updating, retrieving, and managing invoice drafts
*/
export class DraftService {
private static readonly BASE_PATH = "/invoices/draft";
/**
* Create a new draft invoice
* @param dto - Draft creation data
* @returns The created draft
*/
static async createDraft(dto: CreateDraftDto): Promise<DraftInvoice> {
const response = await apiClient.post(this.BASE_PATH, dto);
return response.data;
}
/**
* Get all drafts for the authenticated merchant
* @param page - Page number (1-indexed)
* @param limit - Items per page
* @returns Paginated draft list
*/
static async getDrafts(page = 1, limit = 20): Promise<DraftListResponse> {
const params = new URLSearchParams({
page: String(page),
limit: String(limit),
}).toString();
const response = await apiClient.get(`${this.BASE_PATH}?${params}`);
return response.data;
}
/**
* Get a specific draft by ID
* @param id - Draft UUID
* @returns The draft data
*/
static async getDraft(id: string): Promise<DraftInvoice> {
const response = await apiClient.get(`${this.BASE_PATH}/${id}`);
return response.data;
}
/**
* Update a draft (full update)
* @param id - Draft UUID
* @param updates - Draft update data
* @returns Updated draft
*/
static async updateDraft(id: string, updates: UpdateDraftDto): Promise<DraftInvoice> {
const response = await apiClient.patch(`${this.BASE_PATH}/${id}`, updates);
return response.data;
}
/**
* Auto-save a draft (optimized for frequent calls)
* @param id - Draft UUID
* @param updates - Draft update data
* @returns Updated draft
*/
static async autoSaveDraft(id: string, updates: UpdateDraftDto): Promise<DraftInvoice> {
const response = await apiClient.patch(`${this.BASE_PATH}/${id}/autosave`, {
...updates,
autoSave: true,
});
return response.data;
}
/**
* Convert a draft to a real invoice
* @param id - Draft UUID
* @returns The converted invoice
*/
static async convertDraftToInvoice(id: string): Promise<unknown> {
const response = await apiClient.post(`${this.BASE_PATH}/${id}/convert`);
return response.data;
}
/**
* Discard/delete a draft
* @param id - Draft UUID
* @returns Discard confirmation
*/
static async discardDraft(id: string): Promise<DiscardDraftResponse> {
const response = await apiClient.delete(`${this.BASE_PATH}/${id}`);
return response.data;
}
/**
* Check if a draft has all required fields to be converted to an invoice
* @param draft - Draft invoice data
* @returns Boolean indicating if draft is complete
*/
static isDraftComplete(draft: Partial<DraftInvoice>): boolean {
return !!(draft.clientName?.trim() &&
draft.clientEmail?.trim() &&
draft.amount &&
draft.amount > 0 &&
draft.assetCode);
}
/**
* Get the completion percentage of a draft
* @param draft - Draft invoice data
* @returns Completion percentage (0-100)
*/
static getCompletionPercentage(draft: Partial<DraftInvoice>): number {
const fields = [
{ key: 'invoiceNumber', weight: 1 },
{ key: 'clientName', weight: 1 },
{ key: 'clientEmail', weight: 1 },
{ key: 'description', weight: 0.5 },
{ key: 'amount', weight: 1 },
{ key: 'assetCode', weight: 1 },
{ key: 'dueDate', weight: 0.5 },
];
let totalWeight = 0;
let filledWeight = 0;
for (const field of fields) {
totalWeight += field.weight;
const value = draft[field.key as keyof typeof draft];
if (value !== undefined && value !== null && value !== '') {
filledWeight += field.weight;
}
}
return Math.round((filledWeight / totalWeight) * 100);
}
/**
* Format a draft for display in UI
* @param draft - Draft invoice data
* @returns Formatted draft with computed fields
*/
static formatDraftForDisplay(draft: DraftInvoice): DraftInvoice & {
displayName: string;
displayAmount: string;
hasRequiredFields: boolean;
completionPercentage: number;
} {
const hasRequiredFields = this.isDraftComplete(draft);
const completionPercentage = this.getCompletionPercentage(draft);
return {
...draft,
displayName: draft.clientName || 'Untitled Draft',
displayAmount: draft.amount ? draft.amount.toFixed(2) : '0.00',
hasRequiredFields,
completionPercentage,
};
}
}