Download de "Raw" Billing
Description
Refatorar o Download de Raw do Billing para enviar o arquivo zipado por e-mail caso a fatura tenha muitos itens.
Devem ser alterados os 3 endpoints que utilizamos para baixar dados raws: Admin, View e Invoice (endpoints terminando em raw.csv)
Quando o endpoint é chamado, devemos fazer a query com um COUNT(). Se o resultado tiver mais de 100 mil linhas (double check performance em produção), podemos rodar a query direto com o psql e salvar o resultado direto em disco (algo assim):
psql -h localhost -p 5432 -U carol-billing -d carol-billing -c "COPY (with tenant_apps as (SELECT t.mdm_name as tenant_name, string_agg(app.mdm_name, ', ') as appsFROM batch_raw_carol_app appJOIN batch b ON app.batch_id = b.idJOIN batch_invoice inv ON b.id = inv.batch_idLEFT JOIN batch_raw_tenant t ON t.batch_id = b.id AND t.mdm_id = app.mdm_tenant_idWHERE inv.id = ''GROUP BY 1)SELECTb.cycle_year || '-' || b.cycle_index AS year_month,COALESCE(res.label, it.resource_name) AS resource,res.type,it.tenant_name,t.mdm_id as tenant_id,t.mdm_totvs_customer_code as totvs_customer_code,ta.apps,it.segment,it.organization_name,it.resource_usage_date,SUM(it.quantity) as quantity,SUM(it.total_cost) as total_cost,it.descriptionFROM batch_invoice_item itJOIN batch_invoice invON inv.id = it.batch_invoice_idAND inv.batch_id = it.batch_idJOIN resource resON res.name = it.resource_nameJOIN batch bON b.id = inv.batch_idLEFT JOIN batch_raw_tenant tON t.mdm_name = it.tenant_nameAND t.batch_id = inv.batch_idLEFT JOIN tenant_apps taON ta.tenant_name = it.tenant_nameWHERE inv.id = ''GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13) TO STDOUT WITH CSV HEADER" > output_report.csv
Depois de salvar o arquivo em disco, devemos zipar os dados e enviar para o e-mail do usuário que fez a solicitação.
Quando o arquivo vai ser enviado por e-mail, precisamos também informar o usuário na UI com uma notificação “The invoice details are too large, we will process your request and send you an email with the data in a few minutes.”