javascriptnode.jstypescriptapiaxios

How to delete all records except one record remaining?


I calling all ids with getIds function and use it in clearSales function.clearSales function is running at the end of the test .When I call clearSales all records deleted. I don't want all records deleted, only one record should remain. How to delete all records except one record according to below function?

const getIds= async () => {
    let res = await axios({
        method: 'get',
        url: '/v1/sales'
    })
    expect(res.status).toBe(200)
    const ids = [];
    res.data.salesId.forEach(item => {
        ids.push(item.id);
    });
    return ids
};

export const clearSales = async () => {
    const idList = await getIds()
    let res = await axios({
        method: 'post',
        url: '/v1/feed/bulk_update',
        data: { "feed_ids": idList, "operation": "delete" },
    })
    expect(res.status).toBe(200)
};

Solution

  • Simply delete the id of the record you want to keep from idList. Here an example of implementation:

    export const clearAllOtherSales = async (remainingRecordId) => {
        let idList = await getIds();
        let index = idList.findIndex(id => id === remainingRecordId);
        if (index > -1) idList.splice(index, 1);
        let res = await axios({
            method: 'post',
            url: '/v1/feed/bulk_update',
            data: { "feed_ids": idList, "operation": "delete" },
        });
        expect(res.status).toBe(200);
    }
    

    clearAllOtherSales(115) clear all sales except the one with id 115.