Facing a return type issue while extending customer service list method in medusa js

227 Views Asked by At

I wanna add another field in the customer list response that shows his status that either the customer is active, deactivated, or deleted. My code is sending me a customerStatus field in response with other customer fields but it is showing before insert error on my updatedResult, in the last line return [updatedResult, count].

Here's my piece of code

async listAndCount(
  selector: Selector<Customer> & { q?: string; groups?: string[] },
  config: FindConfig<Customer> = {
    relations: [],
    skip: 0,
    take: 50,
    order: { created_at: "DESC" },
  }
): Promise<[Customer[], number]> {

  const [result, count]: [Customer[], number] = await super.listAndCount(selector, config);
  let customerStatus = '';
 
  const updatedResult = result.map(data => { 

    if(data.isActive === true && data.isDeleted === false)
    customerStatus = 'Active';
    else if (data.isActive === false && data.isDeleted === false)
    customerStatus = 'Deactivated'; 
    else if (data.isActive === false && data.isDeleted === true)
    customerStatus = 'Deleted';

    return { ...data, customerStatus: customerStatus };
  });
  
  return [updatedResult, count] ;
}
}

The error it shows is mentioned below:

Type '{ customerStatus: string; isActive: boolean; isDeleted: boolean; email: string; first_name: string; last_name: string; billing_address_id: string; billing_address: Address; shipping_addresses: Address[]; ... 9 more ...; updated_at: Date; }[]' is not assignable to type 'Customer[]'.
  Property 'beforeInsert' is missing in type '{ customerStatus: string; isActive: boolean; isDeleted: boolean; email: string; first_name: string; last_name: string; billing_address_id: string; billing_address: Address; shipping_addresses: Address[]; ... 9 more ...; updated_at: Date; }' but required in type 'Customer'.ts(2322)

customer.d.ts(18, 13): 'beforeInsert' is declared here.

Also, this beforeInsert method is private in customer class and it doesn't let me change the return type of listAndCount method.

I tried to change the return type and look into the before insert method, but its private. The updatedResult in return statement is showing error but it isn't stopping me running my code.

0

There are 0 best solutions below