#1066 Catch Payer Undefined in AddPayerReference Respomse

This commit is contained in:
Sebastian
2020-09-22 11:44:56 +02:00
parent 7b77e8c234
commit 1aafe81b37

View File

@@ -1,6 +1,9 @@
import { Injectable } from '@angular/core';
import { QueryTokenDTO } from '@swagger/cat';
import { CustomerService as CustomerApiService, PayerService } from '@swagger/crm';
import {
CustomerService as CustomerApiService,
PayerService,
} from '@swagger/crm';
import { map, switchMap, catchError } from 'rxjs/operators';
import { Observable, of, combineLatest, BehaviorSubject } from 'rxjs';
import { CustomerMapping } from '../mappings/customer.mapping';
@@ -28,7 +31,11 @@ import { CustomerFilters } from '../../modules/customer';
})
export class CustomerService {
addAddressError$ = new BehaviorSubject<any>(null);
constructor(private customerService: CustomerApiService, private mapper: CustomerMapping, private payerService: PayerService) {}
constructor(
private customerService: CustomerApiService,
private mapper: CustomerMapping,
private payerService: PayerService
) {}
createCustomer(_customer: User): Observable<User> {
const customer: CustomerDTO = this.mapper.fromUser(_customer);
@@ -97,7 +104,9 @@ export class CustomerService {
filter?: CustomerFilters
): Observable<CustomerSearchResponse> {
const noFilters = filter
? isNullOrUndefined(filter.bonuscard) && isNullOrUndefined(filter.guestaccount) && isNullOrUndefined(filter.onlineshop)
? isNullOrUndefined(filter.bonuscard) &&
isNullOrUndefined(filter.guestaccount) &&
isNullOrUndefined(filter.onlineshop)
? true
: false
: false;
@@ -112,9 +121,15 @@ export class CustomerService {
filter:
filter && !noFilters
? {
['bonuscard']: !isNullOrUndefined(filter.bonuscard) ? String(filter.bonuscard) : undefined,
['guestaccount']: !isNullOrUndefined(filter.guestaccount) ? String(filter.guestaccount) : undefined,
['onlineshop']: !isNullOrUndefined(filter.onlineshop) ? String(filter.onlineshop) : undefined,
['bonuscard']: !isNullOrUndefined(filter.bonuscard)
? String(filter.bonuscard)
: undefined,
['guestaccount']: !isNullOrUndefined(filter.guestaccount)
? String(filter.guestaccount)
: undefined,
['onlineshop']: !isNullOrUndefined(filter.onlineshop)
? String(filter.onlineshop)
: undefined,
}
: undefined,
};
@@ -125,7 +140,9 @@ export class CustomerService {
throw new Error(response.message);
}
return {
customers: response.result.map((t) => this.mapper.customerInfoDTOtoUser(t)),
customers: response.result.map((t) =>
this.mapper.customerInfoDTOtoUser(t)
),
hits: response.hits,
message: response.message,
};
@@ -133,33 +150,44 @@ export class CustomerService {
);
}
getCustomer(customer$: Observable<ResponseArgsOfCustomerDTO>): Observable<User> {
getCustomer(
customer$: Observable<ResponseArgsOfCustomerDTO>
): Observable<User> {
return customer$.pipe(
map((response: ResponseArgsOfCustomerDTO) => {
if (response.error) {
throw new Error(response.message);
}
return {
shippingAddresses: response.result.shippingAddresses.map((addressEnity) => addressEnity.id),
shippingAddresses: response.result.shippingAddresses.map(
(addressEnity) => addressEnity.id
),
user: this.mapper.customerDTOtoUser(response.result),
};
}),
switchMap((customerResult) => {
const user = customerResult.user;
if (!customerResult.shippingAddresses || customerResult.shippingAddresses.length === 0) {
if (
!customerResult.shippingAddresses ||
customerResult.shippingAddresses.length === 0
) {
return of(customerResult.user);
}
const observablesOfShippingAddresses = customerResult.shippingAddresses.map((t) => {
return this.customerService.CustomerGetShippingaddress(t).pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
return response.result;
})
);
});
const shippingAddresses$ = combineLatest(observablesOfShippingAddresses);
const observablesOfShippingAddresses = customerResult.shippingAddresses.map(
(t) => {
return this.customerService.CustomerGetShippingaddress(t).pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
return response.result;
})
);
}
);
const shippingAddresses$ = combineLatest(
observablesOfShippingAddresses
);
return shippingAddresses$.pipe(
switchMap((address: ShippingAddressDTO[]) => {
const updatedUser = user;
@@ -185,34 +213,39 @@ export class CustomerService {
}),
switchMap((customer: User) => {
const updateCustomer = customer;
return this.customerService.CustomerGetAssignedPayersByCustomerId(customer.id).pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
return { customer: customer, assignedPayers: response.result };
}),
switchMap((response) => {
if (Array.isArray(response.assignedPayers) && response.assignedPayers.length > 0) {
const invoiceAddresses = response.assignedPayers.map((t) => {
return {
...this.mapper.fromAssignedPayerDtoToAddress(t),
synced: true,
};
});
const defaultAddress = invoiceAddresses.reduce((ad1, ad2) => {
if (ad1.defaultSince >= ad2.defaultSince) {
return ad1;
}
return ad2;
});
updateCustomer.poossible_invoice_addresses = invoiceAddresses;
updateCustomer.invoice_address = defaultAddress;
}
return this.customerService
.CustomerGetAssignedPayersByCustomerId(customer.id)
.pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
return { customer: customer, assignedPayers: response.result };
}),
switchMap((response) => {
if (
Array.isArray(response.assignedPayers) &&
response.assignedPayers.length > 0
) {
const invoiceAddresses = response.assignedPayers.map((t) => {
return {
...this.mapper.fromAssignedPayerDtoToAddress(t),
synced: true,
};
});
const defaultAddress = invoiceAddresses.reduce((ad1, ad2) => {
if (ad1.defaultSince >= ad2.defaultSince) {
return ad1;
}
return ad2;
});
updateCustomer.poossible_invoice_addresses = invoiceAddresses;
updateCustomer.invoice_address = defaultAddress;
}
return of(updateCustomer);
})
);
return of(updateCustomer);
})
);
})
);
}
@@ -231,7 +264,9 @@ export class CustomerService {
throw new Error(response.message);
}
return {
customers: response.result.map((t) => this.mapper.customerInfoDTOtoUser(t)),
customers: response.result.map((t) =>
this.mapper.customerInfoDTOtoUser(t)
),
hits: 0,
};
})
@@ -259,28 +294,32 @@ export class CustomerService {
isDefault: true,
};
if (shippingAddress.synced === true) {
return this.customerService.CustomerModifyShippingAddressFlag(params).pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
return response.result;
})
);
return this.customerService
.CustomerModifyShippingAddressFlag(params)
.pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
return response.result;
})
);
} else {
return this.addShippingAddress(customerId, {
...shippingAddress,
id: null,
}).pipe(
switchMap(() => {
return this.customerService.CustomerModifyShippingAddressFlag(params).pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
return response.result;
})
);
return this.customerService
.CustomerModifyShippingAddressFlag(params)
.pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
return response.result;
})
);
})
);
}
@@ -332,7 +371,10 @@ export class CustomerService {
if (!isNullOrUndefined(shippingAddress.id)) {
const params = {
shippingAddressId: shippingAddress.id,
shippingAddress: this.mapper.fromAddressToShippingAddressDTO(shippingAddress, !isNullOrUndefined(shippingAddress.id)),
shippingAddress: this.mapper.fromAddressToShippingAddressDTO(
shippingAddress,
!isNullOrUndefined(shippingAddress.id)
),
customerId: customerId,
};
return this.customerService.CustomerUpdateShippingAddress(params).pipe(
@@ -360,7 +402,10 @@ export class CustomerService {
);
} else {
const params = {
shippingAddress: this.mapper.fromAddressToShippingAddressDTO(shippingAddress, !isNullOrUndefined(shippingAddress.id)),
shippingAddress: this.mapper.fromAddressToShippingAddressDTO(
shippingAddress,
!isNullOrUndefined(shippingAddress.id)
),
customerId: customerId,
};
return this.customerService.CustomerCreateShippingAddress(params).pipe(
@@ -420,15 +465,27 @@ export class CustomerService {
payerId: payerResponse.result.id,
isDefault: true,
};
return this.customerService.CustomerAddPayerReference(referenceParamsparams).pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
this.addAddressError$.next(undefined);
return response.result.payer.data;
})
);
return this.customerService
.CustomerAddPayerReference(referenceParamsparams)
.pipe(
map((response) => {
if (response.error) {
throw new Error(response.message);
}
if (!response.result || !response.result.payer) {
throw new Error(
response.message ||
'Beim Anlegen der Rechnungsadress-Referenz ist ein Fehler aufgetreten.'
);
}
this.addAddressError$.next(undefined);
return response.result.payer.data;
}),
catchError((err) => {
throw new Error(err.message);
})
);
})
);
} else {
@@ -466,7 +523,10 @@ export class CustomerService {
if (response.error) {
throw new Error(response.message);
}
return response.result.map((t, index) => <Country>{ index, id: t.id, key: t.isO3166_A_3, value: t.name });
return response.result.map(
(t, index) =>
<Country>{ index, id: t.id, key: t.isO3166_A_3, value: t.name }
);
})
);
}