forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-template.service.ts
More file actions
219 lines (186 loc) · 8.2 KB
/
Copy pathsplit-template.service.ts
File metadata and controls
219 lines (186 loc) · 8.2 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
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
import { InjectDataSource, InjectRepository } from "@nestjs/typeorm";
import { DataSource } from "typeorm";
import { SplitTemplate } from "./entities/split-template.entity";
import { Repository } from "typeorm";
import { CreateSplitTemplateDto } from "./dto/create-split-template.dto";
import { UpdateSplitTemplateDto } from "./dto/update-split-template.dto";
import { CreateSplitFromTemplateDto } from "./dto/create-split-from-template.dto";
import { SplitsService } from "../modules/splits/splits.service";
import { CreateItemDto, CreateParticipantDto, CreateSplitDto } from "../modules/splits/dto/split.dto";
import { Split } from "../entities/split.entity";
@Injectable()
export class SplitTemplateService {
constructor(
@InjectRepository(SplitTemplate)
private readonly repo: Repository<SplitTemplate>,
private readonly splitsService: SplitsService,
@InjectDataSource()
private readonly dataSource: DataSource,
) {}
create(userId: string, dto: CreateSplitTemplateDto) {
const template = this.repo.create({ ...dto, userId });
return this.repo.save(template);
}
findAllForUser(userId: string) {
return this.repo.find({ where: { userId } });
}
findOne(id: string) {
return this.repo.findOneBy({ id });
}
update(id: string, dto: UpdateSplitTemplateDto) {
return this.repo.update(id, dto);
}
delete(id: string) {
return this.repo.delete(id);
}
async createSplitFromTemplate(
templateId: string,
dto?: CreateSplitFromTemplateDto,
): Promise<Split> {
return this.dataSource.transaction(async (manager: any) => {
const templateRepo = manager.getRepository(SplitTemplate);
const template = await templateRepo.findOneBy({ id: templateId });
if (!template) throw new NotFoundException("Template not found");
// Increment usage inside the same transaction as split creation.
await templateRepo.increment({ id: templateId }, "usageCount", 1);
const participantsInput =
dto?.participantOverrides ?? template.defaultParticipants ?? [];
const itemsInput =
dto?.itemOverrides ?? template.defaultItems ?? [];
const description =
dto?.customName ?? template.description ?? template.name;
const taxPercentage = Number(template.taxPercentage ?? 0);
const tipPercentage = Number(template.tipPercentage ?? 0);
const normalizedItems = this.normalizeItems(itemsInput);
const subtotal = normalizedItems.reduce(
(sum, i) => sum + Number(i.totalPrice),
0,
);
const taxAmount = subtotal * (taxPercentage / 100);
const tipAmount = subtotal * (tipPercentage / 100);
const totalAmount = this.round2(subtotal + taxAmount + tipAmount);
const normalizedParticipants = this.normalizeParticipants(
participantsInput,
totalAmount,
template.defaultParticipants ?? [],
);
const createSplitDto: CreateSplitDto = {
totalAmount,
description,
creatorWalletAddress: template.userId,
// preferredCurrency defaults inside SplitsService
participants: normalizedParticipants,
items: normalizedItems,
};
return this.splitsService.createSplit(createSplitDto, manager);
});
}
private round2(value: number): number {
return Math.round(value * 100) / 100;
}
private normalizeItems(items: any[] | undefined): CreateItemDto[] {
const normalized = (items ?? []).map((item) => {
const name = String(item?.name ?? item?.itemName ?? item?.title ?? "");
if (!name) throw new BadRequestException("Item.name is required");
const quantity = Number(item?.quantity ?? item?.qty ?? 1);
const unitPrice = Number(
item?.unitPrice ?? item?.price ?? item?.unit_price ?? 0,
);
const totalPrice = Number(
item?.totalPrice ?? item?.total ?? unitPrice * quantity,
);
// In the current backend, item allocations are stored as Participant IDs
// (resolved later). Template items typically don't include those IDs.
const assignedToIds =
item?.assignedToIds ??
item?.participantIds ??
item?.assignedTo ??
[];
return {
name,
quantity,
unitPrice,
totalPrice,
category: item?.category,
assignedToIds,
} as CreateItemDto;
});
return normalized;
}
private normalizeParticipants(
participants: any[] | undefined,
totalAmount: number,
fallbackParticipants: any[] = [],
): CreateParticipantDto[] {
const list = participants ?? [];
if (list.length === 0) return [];
const hasExplicitAmounts = list.some(
(p) => typeof p?.amountOwed === "number" && !Number.isNaN(p.amountOwed),
);
const round2 = (v: number) => Math.round(v * 100) / 100;
// First pass: compute weights.
let weights: number[];
if (hasExplicitAmounts) {
weights = list.map((p) => Number(p.amountOwed ?? 0));
const sumWeights = weights.reduce((s, w) => s + w, 0);
if (sumWeights > 0) {
weights = weights.map((w) => (w / sumWeights) * totalAmount);
} else {
weights = list.map(() => round2(totalAmount / list.length));
}
} else {
weights = list.map((p) => {
const share =
p?.share ??
p?.percentage ??
p?.pct ??
p?.weight ??
undefined;
return share === undefined || share === null ? NaN : Number(share);
});
const validWeights = weights.filter((w) => !Number.isNaN(w));
const sumShares = validWeights.reduce((s, w) => s + w, 0);
if (sumShares > 0) {
weights = weights.map((weight) =>
!Number.isNaN(weight)
? (weight / sumShares) * totalAmount
: 0,
);
} else {
weights = list.map(() => round2(totalAmount / list.length));
}
}
// Second pass: rounding adjustment so participant totals sum exactly.
const allocated = weights.map((w) => round2(w));
const allocatedSum = allocated.reduce((s, a) => s + a, 0);
const delta = round2(totalAmount - allocatedSum);
if (Math.abs(delta) > 0.001 && allocated.length > 0) {
// Apply adjustment to the largest allocated participant.
let maxIdx = 0;
for (let i = 1; i < allocated.length; i++) {
if (allocated[i] > allocated[maxIdx]) maxIdx = i;
}
allocated[maxIdx] = round2(allocated[maxIdx] + delta);
}
return list.map((p, idx) => {
const explicitId = p?.userId ?? p?.walletAddress ?? p?.id;
const fallback =
fallbackParticipants[idx] ??
fallbackParticipants.find(
(fp) => fp?.name && p?.name && fp.name === p.name,
);
const userId = String(explicitId ?? fallback?.userId ?? fallback?.walletAddress ?? fallback?.id ?? "");
if (!userId || userId === "undefined" || userId === "null") {
throw new BadRequestException(
"Each template participant must include userId/walletAddress (or be mappable from template defaults)",
);
}
return {
userId,
walletAddress: p?.walletAddress ?? fallback?.walletAddress ?? userId,
amountOwed: allocated[idx] ?? 0,
} as CreateParticipantDto;
});
}
}