Files
ISA-Frontend/libs/crm/data-access/src/lib/resources/customer-bonus-cards.resource.ts
Lorenz Hilpert 37840b1565 Merged PR 1962: Reward Shopping Cart
Related work items: #5305, #5356, #5357, #5359
2025-09-30 14:50:01 +00:00

50 lines
1.3 KiB
TypeScript

import { effect, inject, Injectable, resource, signal } from '@angular/core';
import { CrmSearchService, CrmTabMetadataService } from '../services';
import { TabService } from '@isa/core/tabs';
@Injectable()
export class CustomerBonusCardsResource {
#customerService = inject(CrmSearchService);
#params = signal<{ customerId: number | undefined }>({
customerId: undefined,
});
params(params: { customerId?: number }) {
this.#params.update((p) => ({ ...p, ...params }));
}
readonly resource = resource({
params: () => this.#params(),
loader: async ({ params, abortSignal }) => {
if (!params.customerId) {
return undefined;
}
const res = await this.#customerService.fetchCustomerCards(
{
customerId: params.customerId,
},
abortSignal,
);
return res.result;
},
});
}
@Injectable()
export class SelectedCustomerBonusCardsResource extends CustomerBonusCardsResource {
#tabId = inject(TabService).activatedTabId;
#customerMetadata = inject(CrmTabMetadataService);
constructor() {
super();
effect(() => {
const tabId = this.#tabId();
const customerId = tabId
? this.#customerMetadata.selectedCustomerId(tabId)
: undefined;
this.params({ customerId });
});
}
}