Skip to content

Commit 7652af0

Browse files
committed
Transform value to corresponding dropdown item's key #758
1 parent ecb71c3 commit 7652af0

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/app/import/services/subscription-details/import-upload-subscription-details.service.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ describe("ImportUploadSubscriptionDetailsService", () => {
117117
total: 3,
118118
});
119119
});
120+
121+
it("sends `Key` of corresponding dropdown item as value", async () => {
122+
const entry = buildEntry(
123+
{ eventId: 11, subscriptionDetailId: 1001, value: "Apple" },
124+
{ validationStatus: "valid", importStatus: null },
125+
);
126+
entry.data.subscriptionDetail!.DropdownItems = [
127+
{ Key: 10, Value: "Apple", IsActive: true },
128+
{ Key: 11, Value: "Pear", IsActive: true },
129+
];
130+
131+
await service.upload([entry]);
132+
133+
expect(subscriptionDetailsServiceMock.update).toHaveBeenCalledTimes(1);
134+
135+
const value =
136+
subscriptionDetailsServiceMock.update.calls.mostRecent().args[1];
137+
expect(value).toBe(10);
138+
});
120139
});
121140

122141
function buildEntry(

src/app/import/services/subscription-details/import-upload-subscription-details.service.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ export class ImportUploadSubscriptionDetailsService {
9292
private persistEntry(
9393
entry: SubscriptionDetailImportEntry,
9494
): Observable<SubscriptionDetailImportEntry> {
95-
if (entry.entry.value === entry.data.subscriptionDetail?.Value) {
95+
if (
96+
this.getNormalizedValue(entry) == entry.data.subscriptionDetail?.Value
97+
) {
9698
// Ignore entry with an unchanged value
9799
this.markSuccessEntry(entry);
98100
return of(entry);
@@ -114,15 +116,14 @@ export class ImportUploadSubscriptionDetailsService {
114116
);
115117
}
116118

117-
private updateSubscriptionDetail({
118-
entry,
119-
data: { subscriptionDetail },
120-
}: SubscriptionDetailImportEntry): Observable<void> {
121-
if (!subscriptionDetail)
119+
private updateSubscriptionDetail(
120+
entry: SubscriptionDetailImportEntry,
121+
): Observable<void> {
122+
if (!entry.data.subscriptionDetail)
122123
return throwError(
123124
() =>
124125
new Error(
125-
`Subscription not present for entry: ${JSON.stringify(entry)}`,
126+
`Subscription not present for entry: ${JSON.stringify(entry.entry)}`,
126127
),
127128
);
128129

@@ -136,15 +137,30 @@ export class ImportUploadSubscriptionDetailsService {
136137
// return of(undefined).pipe(delay(500 + (Math.random() - 0.5) * 300));
137138

138139
return this.subscriptionDetailsService.update(
139-
subscriptionDetail,
140+
entry.data.subscriptionDetail,
140141
// eslint-disable-next-line @typescript-eslint/no-explicit-any
141-
entry.value as any,
142+
this.getNormalizedValue(entry) as any,
142143
new HttpContext().set(RestErrorInterceptorOptions, {
143144
disableErrorHandling: true,
144145
}),
145146
);
146147
}
147148

149+
private getNormalizedValue({
150+
entry,
151+
data: { subscriptionDetail },
152+
}: SubscriptionDetailImportEntry): unknown {
153+
if (Array.isArray(subscriptionDetail?.DropdownItems)) {
154+
const item = subscriptionDetail.DropdownItems.find(
155+
(item) => item.Value === entry.value,
156+
);
157+
if (item) {
158+
return item.Key;
159+
}
160+
}
161+
return entry.value;
162+
}
163+
148164
private markSuccessEntry(entry: SubscriptionDetailImportEntry): void {
149165
entry.importStatus = "success";
150166
entry.importError = null;

0 commit comments

Comments
 (0)