| 46 | */ |
| 47 | |
| 48 | export const formatCsvFile = (file) => { |
| 49 | const LINE_BREAK = '\n'; |
| 50 | const lines = file.split(LINE_BREAK); |
| 51 | |
| 52 | lines.shift(); // Remove a primeira linha que é o cabeçalho (Lista de participantes ativos do Pix) |
| 53 | const headers = lines |
| 54 | .shift() |
| 55 | .split(';') |
| 56 | .map((header) => header.toLowerCase().replace(/ /g, '')); |
| 57 | |
| 58 | const expectedHeaders = [ |
| 59 | 'nomereduzido', |
| 60 | 'ispb', |
| 61 | 'modalidadedeparticipaçãonopix', |
| 62 | 'tipodeparticipaçãonospi', |
| 63 | ]; |
| 64 | |
| 65 | if (!isEqual(expectedHeaders, headers)) { |
| 66 | throw new BaseError({ |
| 67 | status: 500, |
| 68 | message: 'Dados e/ou cabeçalho do arquivo estão diferentes/atualizados', |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | return lines |
| 74 | .map((line) => line.split(';')) |
| 75 | .filter(([ispb]) => ispb) |
| 76 | .map((data) => { |
| 77 | return { |
| 78 | ispb: data[2], |
| 79 | nome: data[1], |
| 80 | nome_reduzido: data[1], |
| 81 | modalidade_participacao: data[8], |
| 82 | tipo_participacao: data[6], |
| 83 | inicio_operacao: null, |
| 84 | }; |
| 85 | }) |
| 86 | .filter(Boolean); |
| 87 | } catch (error) { |
| 88 | throw new BaseError({ |
| 89 | status: 500, |
| 90 | message: 'Erro ao fazer o parse dos dados', |
| 91 | }); |
| 92 | } |
| 93 | }; |