mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-28 22:42:11 +01:00
Merged PR 1990: feat(crm-data-access,checkout): improve primary bonus card selection logic
feat(crm-data-access,checkout): improve primary bonus card selection logic Enhance getPrimaryBonusCard helper to sort cards alphabetically by code when multiple primary cards exist or when no primary card is designated. This ensures deterministic card selection across the application. Add comprehensive test coverage for edge cases including: - Multiple primary cards (returns first alphabetically) - No primary cards (returns first alphabetically) - Empty bonus cards array (returns undefined) Add TODO comments in ShoppingCartService for future refactoring of cart handling logic to improve code organization and reusability. Ref: #5407
This commit is contained in:
committed by
Lorenz Hilpert
parent
6db5f2afda
commit
f87d3a35d9
@@ -173,6 +173,7 @@ export class ShoppingCartService {
|
||||
return res.result as ShoppingCart;
|
||||
}
|
||||
|
||||
// TODO: Code Kommentieren + Beschreiben
|
||||
async completeRewardSelection({
|
||||
tabId,
|
||||
rewardSelectionItems,
|
||||
@@ -222,6 +223,7 @@ export class ShoppingCartService {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: If Logik in eigene Funktionen auslagern - Für Remove die Update Funktion mit Quantity 0 nutzen, Code Kommentieren
|
||||
async #handleCart({
|
||||
shoppingCartId,
|
||||
itemId,
|
||||
@@ -295,6 +297,7 @@ export class ShoppingCartService {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: If Logik in eigene Funktionen auslagern - Für Remove die Update Funktion mit Quantity 0 nutzen, Code Kommentieren
|
||||
async #handleRewardCart({
|
||||
rewardShoppingCartId,
|
||||
itemId,
|
||||
|
||||
@@ -7,6 +7,7 @@ describe('getPrimaryBonusCard', () => {
|
||||
// Arrange
|
||||
const bonusCards: BonusCardInfo[] = [
|
||||
{
|
||||
code: 'CARD-B',
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
isActive: true,
|
||||
@@ -14,19 +15,13 @@ describe('getPrimaryBonusCard', () => {
|
||||
totalPoints: 100,
|
||||
} as BonusCardInfo,
|
||||
{
|
||||
code: 'CARD-A',
|
||||
firstName: 'Jane',
|
||||
lastName: 'Smith',
|
||||
isActive: true,
|
||||
isPrimary: true,
|
||||
totalPoints: 200,
|
||||
} as BonusCardInfo,
|
||||
{
|
||||
firstName: 'Bob',
|
||||
lastName: 'Johnson',
|
||||
isActive: true,
|
||||
isPrimary: false,
|
||||
totalPoints: 50,
|
||||
} as BonusCardInfo,
|
||||
];
|
||||
|
||||
// Act
|
||||
@@ -35,14 +30,14 @@ describe('getPrimaryBonusCard', () => {
|
||||
// Assert
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.isPrimary).toBe(true);
|
||||
expect(result?.firstName).toBe('Jane');
|
||||
expect(result?.lastName).toBe('Smith');
|
||||
expect(result?.code).toBe('CARD-A');
|
||||
});
|
||||
|
||||
it('should return undefined when no primary bonus card exists', () => {
|
||||
it('should return first alphabetically when no primary card exists', () => {
|
||||
// Arrange
|
||||
const bonusCards: BonusCardInfo[] = [
|
||||
{
|
||||
code: 'CARD-C',
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
isActive: true,
|
||||
@@ -50,6 +45,7 @@ describe('getPrimaryBonusCard', () => {
|
||||
totalPoints: 100,
|
||||
} as BonusCardInfo,
|
||||
{
|
||||
code: 'CARD-A',
|
||||
firstName: 'Jane',
|
||||
lastName: 'Smith',
|
||||
isActive: true,
|
||||
@@ -62,7 +58,8 @@ describe('getPrimaryBonusCard', () => {
|
||||
const result = getPrimaryBonusCard(bonusCards);
|
||||
|
||||
// Assert
|
||||
expect(result).toBeUndefined();
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.code).toBe('CARD-A');
|
||||
});
|
||||
|
||||
it('should return undefined when bonus cards array is empty', () => {
|
||||
@@ -76,10 +73,11 @@ describe('getPrimaryBonusCard', () => {
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return the first primary card when multiple primary cards exist', () => {
|
||||
it('should return first alphabetically when multiple primary cards exist', () => {
|
||||
// Arrange
|
||||
const bonusCards: BonusCardInfo[] = [
|
||||
{
|
||||
code: 'CARD-Z',
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
isActive: true,
|
||||
@@ -87,6 +85,7 @@ describe('getPrimaryBonusCard', () => {
|
||||
totalPoints: 100,
|
||||
} as BonusCardInfo,
|
||||
{
|
||||
code: 'CARD-A',
|
||||
firstName: 'Jane',
|
||||
lastName: 'Smith',
|
||||
isActive: true,
|
||||
@@ -101,6 +100,6 @@ describe('getPrimaryBonusCard', () => {
|
||||
// Assert
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.isPrimary).toBe(true);
|
||||
expect(result?.firstName).toBe('John');
|
||||
expect(result?.code).toBe('CARD-A');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,22 @@
|
||||
import { BonusCardInfo } from '../models';
|
||||
|
||||
export function getPrimaryBonusCard(bonusCards: BonusCardInfo[]) {
|
||||
return bonusCards.find((card) => card.isPrimary);
|
||||
export function getPrimaryBonusCard(
|
||||
bonusCards: BonusCardInfo[],
|
||||
): BonusCardInfo | undefined {
|
||||
if (bonusCards.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Filter primary cards if any exist
|
||||
const primaryCards = bonusCards.filter((card) => card.isPrimary);
|
||||
|
||||
// Use primary cards if available, otherwise use all cards
|
||||
const cardsToSort = primaryCards.length > 0 ? primaryCards : bonusCards;
|
||||
|
||||
// Sort alphabetically by code and return the first one
|
||||
return cardsToSort.sort((a, b) => {
|
||||
const codeA = a.code?.toLowerCase() ?? '';
|
||||
const codeB = b.code?.toLowerCase() ?? '';
|
||||
return codeA.localeCompare(codeB);
|
||||
})[0];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user