#Nagual

1 messages · Page 1 of 1 (latest)

subtle belfryBOT
west cave
#

Hi Jack

#

And this is code prior to calling create item lines

    const itemsGroupedByItemType: ItemsByItemType = itemsToProcess.reduce<{ [itemTypeId: string]: LeanDocument<ItemDocument>[] }>((acc, item) => {
      const itemTypeId = item.itemType.toString();
      if (!acc[itemTypeId]) {
        acc[itemTypeId] = [];
      }
      acc[itemTypeId].push(item);
      return acc;
    }, {});

    const { invoiceUrl, invoiceId, clientStripeId } = await this.paymentService.createDraftInvoice({ clientId });

    for (const itemTypeId in itemsGroupedByItemType) {
      const items = itemsGroupedByItemType[itemTypeId];
      this.logger.log(`Item being charged: ${JSON.stringify(items, null, 2)}`);
      await this.paymentService.createLineItems({ clientStripeId, invoiceId, items });
    }

limber juniper
#

Can you tell what you expect and what you get?

west cave
#

I expect to add multiple line items to one draft invoice, but right now what is happening it creates one line item and adds all quantities to it

#

Each line should be a different product in stripe

#

This is my customer right now

#

And If I want to charge this invoice right now

#

Everything will end up in first item

#

Like it happened here

limber juniper
#

What does itemCount resolve to?

west cave
#

Qty that you see in query

#

I group items by itemType, pass that in a loop

#

count them for qty

limber juniper
west cave
#

req_luKWTVOER6U2pB

Here everything is already groped in one lineItem

limber juniper
#

OK, so I saw you've sent a request to create a line item of 2525 quality, is it what you intent to do?

west cave
#

No, in my logs it was shown that multiple itemTypes were sent to the loop

#

Is it possible that invoiceItems.create doesn't create one item at a time?

#

and it waits until I do invoice.pay?

limber juniper
#

Does itemsGroupedByItemType() return a new array? or it basically just adding new items to acc array?

west cave
#

What it does it creates new object:

{itemTypeId: [items], itemTypeId:[items]}

limber juniper
#

I don't see any other POST request to create another invoice item.

west cave
#

I'm setting new logs in the flow to follow and make sure proper items are passed. I'll write back in maybe 10 minutes

#

Is that ok?

limber juniper
#

There's a possibility that your code only execute the loop once, and therefore only one invoice item is added.

#

I'd suggest you to put more logs in your code and debug.

#

Sure, let me know if you need any other help when you are back

west cave
#

Thank you Jack

west cave
#

Jack just letting you, you were spot on.
Issue was in itemsGroupedByItemType.
const itemType = item.itemType as any as ItemTypeDocument;
const itemTypeId: string = itemType._id.toString();

This solved the issue

#

It was casting itemTypeId to object instead of string and than it just put all in the same array