Segregate array based on same value and find the sum of number fields

I have an array of object how to segregate it based on same value and then find sum of other elements. I have tried with reduce but it didn’t help

givenArray = [
    {
        id: 'ABC001', debtAmt: 55000, totalPaid: 1000, principalDue: 12000, principalPaid: 180, interestDue: 15000, interestPaid: 70, penaltyDue: 17000, penaltyPaid: 500,
        subRows: [
            { id: 'ABC001', code: 'AL', debtAmt: 20000, totalPaid: 100, principalDue: 5000, principalPaid: 20, interestDue: 1000, interestPaid: 20, penaltyDue: 5000, penaltyPaid: 150 },
            { id: 'ABC001', code: 'AL', debtAmt: 30000, totalPaid: 200, principalDue: 4000, principalPaid: 30, interestDue: 5000, interestPaid: 20, penaltyDue: 5000, penaltyPaid: 250 },
            { id: 'ABC001', code: 'HL', debtAmt: 2000, totalPaid: 300, principalDue: '', principalPaid: 50, interestDue: '', interestPaid: 10, penaltyDue: 4000, penaltyPaid: '' },
            { id: 'ABC001', code: 'HL', debtAmt: 3000, totalPaid: 400, principalDue: 3000, principalPaid: 80, interestDue: 9000, interestPaid: 20, penaltyDue: 3000, penaltyPaid: 200 }
        ]
    },

    {
        id: 'DEF123', debtAmt: 20000, totalPaid: 800, principalDue: 2650, principalPaid: 160, interestDue: 2400, interestPaid: 1150, penaltyDue: 3600, penaltyPaid: 1300,
        subRows: [
            { id: 'DEF123', code: 'HL', debtAmt: 2000, totalPaid: '', principalDue: 2000, principalPaid: 10, interestDue: 1000, interestPaid: 50, penaltyDue: 2000, penaltyPaid: 100 },
            { id: 'DEF123', code: 'DL', debtAmt: 3000, totalPaid: '', principalDue: 500, principalPaid: 20, interestDue: 500, interestPaid: 100, penaltyDue: 1000, penaltyPaid: 150 },
            { id: 'DEF123', code: 'DL', debtAmt: 4000, totalPaid: 100, principalDue: 100, principalPaid: 30, interestDue: 200, interestPaid: 200, penaltyDue: 500, penaltyPaid: 250 },
            { id: 'DEF123', code: 'DL', debtAmt: 5000, totalPaid: 200, principalDue: '', principalPaid: 40, interestDue: 300, interestPaid: 300, penaltyDue: '', penaltyPaid: 300 },
            { id: 'DEF123', code: 'AL', debtAmt: 6000, totalPaid: 500, principalDue: 50, principalPaid: 60, interestDue: 400, interestPaid: 500, penaltyDue: 100, penaltyPaid: 500 }
        ]
    },

    {
        id: 'XYZ001', debtAmt: 1200, totalPaid: 1500, principalDue: 1500, principalPaid: 300, interestDue: 1200, interestPaid: 500, penaltyDue: 200, penaltyPaid: 100,
        subRows: [
            { id: 'XYZ001', code: 'PL', debtAmt: 500, totalPaid: 600, principalDue: 800, principalPaid: 200, interestDue: 900, interestPaid: 500, penaltyDue: 200, penaltyPaid: 100 },
            { id: 'XYZ001', code: 'DL', debtAmt: 700, totalPaid: 900, principalDue: 700, principalPaid: 100, interestDue: 300, interestPaid: '', penaltyDue: '', penaltyPaid: '' },
        ]
    },
];

result = [
    {
        id: 'ABC001', debtAmt: 55000, totalPaid: 1000, principalDue: 12000, principalPaid: 180, interestDue: 15000, interestPaid: 70, penaltyDue: 17000, penaltyPaid: 500,
        subRows: [
            { id: 'ABC001', code: 'AL', debtAmt: 50000, totalPaid: 300, principalDue: 9000, principalPaid: 50, interestDue: 6000, interestPaid: 40, penaltyDue: 10000, penaltyPaid: 400 },
            { id: 'ABC001', code: 'HL', debtAmt: 5000, totalPaid: 700, principalDue: 3000, principalPaid: 130, interestDue: 9000, interestPaid: 30, penaltyDue: 7000, penaltyPaid: 200 },
        ]
    },

    {
        id: 'DEF123', debtAmt: 20000, totalPaid: 800, principalDue: 2650, principalPaid: 160, interestDue: 2400, interestPaid: 1150, penaltyDue: 3600, penaltyPaid: 1300,
        subRows: [
            { id: 'DEF123', code: 'HL', debtAmt: 2000, totalPaid: '', principalDue: 2000, principalPaid: 10, interestDue: 1000, interestPaid: 50, penaltyDue: 2000, penaltyPaid: 100 },
            { id: 'DEF123', code: 'DL', debtAmt: 12000, totalPaid: 300, principalDue: 600, principalPaid: 90, interestDue: 1000, interestPaid: 600, penaltyDue: 1500, penaltyPaid: 700 },
            { id: 'DEF123', code: 'AL', debtAmt: 6000, totalPaid: 500, principalDue: 50, principalPaid: 60, interestDue: 400, interestPaid: 500, penaltyDue: 100, penaltyPaid: 500 }
        ]
    },

    {
        id: 'XYZ001', debtAmt: 1200, totalPaid: 1500, principalDue: 1500, principalPaid: 300, interestDue: 1200, interestPaid: 500, penaltyDue: 200, penaltyPaid: 100,
        subRows: [
            { id: 'XYZ001', code: 'PL', debtAmt: 500, totalPaid: 600, principalDue: 800, principalPaid: 200, interestDue: 900, interestPaid: 500, penaltyDue: 200, penaltyPaid: 100 },
            { id: 'XYZ001', code: 'DL', debtAmt: 700, totalPaid: 900, principalDue: 700, principalPaid: 100, interestDue: 300, interestPaid: '', penaltyDue: '', penaltyPaid: '' },
        ]
    },
]

const res = givenArray.map(el => Object.values(el.subRows?.reduce((acc, curr) => {
    const { id, code, ...rest } = curr;
    acc[code] = acc[code] || { id, code, ...rest };
    Object.entries(rest).forEach(([k, v]) => {
        acc[code][k] = acc[code][k] || 0
        if (v !== '') acc[code][k] += v
    });
    return acc
}, {})))