here is the function that i've implemented:
const cusRepo = this.manager.getCustomRepository(this.customerRepository);
let cgId = '';
let existingCusGroup = [];
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const customerGroupsInfo = await cusRepo
.createQueryBuilder('cus')
.where('cus.id = :customerId', { customerId: offer.buyer_id })
.innerJoinAndSelect('cus.groups', 'groups')
.innerJoinAndSelect('groups.customers', 'customers')
.getMany();
if (customerGroupsInfo && customerGroupsInfo.length == 1) {
existingCusGroup = customerGroupsInfo[0].groups.filter(group => group.customers.length == 1);
}
if (existingCusGroup.length) {
cgId = existingCusGroup[0].id
}
else {
const newCusGroup = await this.customerGroupService.create({
name: "offer_" + offer.buyer_id // doesn't matter what name we're using since its not going to get used or displayed anywhere
})
await this.customerGroupService.addCustomers(newCusGroup.id, [offer.buyer_id])
cgId = newCusGroup.id
}
const priceList = await this.priceListService.create({
name: "offer_" + offer.buyer_id, // same here. we're not going to use the name
description: "this is an offer price for " + offer.buyer_id + " user.",
type: PriceListType.SALE,
status: PriceListStatus.ACTIVE,
customer_groups: [{
id: cgId
}],
prices: [{
amount: offer.offer_value,
variant_id: offer.item,
currency_code: 'sar',
}],
// starts_at: new Date(),
// ends_at: tomorrow
})
return priceList.id
}```