|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | +const { parse } = require("csv-parse"); |
| 4 | +const { stringify } = require("csv-stringify"); |
| 5 | + |
| 6 | +async function loadCsv(filename) { |
| 7 | + const filePath = path.join(__dirname, "../data/", filename); |
| 8 | + const data = []; |
| 9 | + return new Promise((resolve, reject) => { |
| 10 | + fs.createReadStream(filePath) |
| 11 | + .pipe(parse({ columns: true, trim: true })) |
| 12 | + .on("data", (row) => data.push(row)) |
| 13 | + .on("end", () => resolve(data)) |
| 14 | + .on("error", (err) => reject(err)); |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +function manipulateData(data) { |
| 19 | + const pupilCounts = {}; |
| 20 | + data.forEach((row) => { |
| 21 | + const pupilKey = row.Pupils; |
| 22 | + pupilCounts[pupilKey] = (pupilCounts[pupilKey] || 0) + 1; |
| 23 | + }); |
| 24 | + data.forEach((row) => { |
| 25 | + row.TicketCount = pupilCounts[row.Pupils]; |
| 26 | + }); |
| 27 | + return data; |
| 28 | +} |
| 29 | + |
| 30 | +function saveCsvToOutFolder(data, filename) { |
| 31 | + const filePath = path.join(__dirname, "../data/", filename); |
| 32 | + const outFilePath = path.join(__dirname, "../out", path.basename(filePath)); |
| 33 | + stringify(data, { header: true }, (err, outputCsv) => { |
| 34 | + console.log("\nSaving updated CSV to", outFilePath); |
| 35 | + |
| 36 | + if (err) throw err; |
| 37 | + fs.writeFileSync(outFilePath, outputCsv); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +function expandPupils(data, years) { |
| 42 | + const expanded = []; |
| 43 | + return data.map((row) => { |
| 44 | + let retPupil = "Unknown, check other column"; |
| 45 | + |
| 46 | + const pupils = row.Pupils.split("&").filter((p) => |
| 47 | + years.some((year) => p.includes(year)) |
| 48 | + ); |
| 49 | + |
| 50 | + if ( |
| 51 | + // row['Guest first name'] === 'Kelly' |
| 52 | + row['Guest first name'] === 'Micaela' |
| 53 | + ) { |
| 54 | + console.log('-----------------'); |
| 55 | + console.log(`Pupils: ${row.Pupils}, Found: ${pupils}, Tickets: ${row.TicketCount}`); |
| 56 | + console.log('-----------------'); |
| 57 | + } |
| 58 | + |
| 59 | + if (pupils.length > 1) { |
| 60 | + console.log(`Multiple pupils found for row ${row.Pupils}`); |
| 61 | + } |
| 62 | + |
| 63 | + for (const pupil of pupils) { |
| 64 | + if (expanded.some((e) => e.Pupil === pupil && row['Guest first name'] === e['Guest first name'])) { |
| 65 | + console.log(`Pupil ${pupil} already exists`); |
| 66 | + } else { |
| 67 | + console.log(`Adding Pupil ${pupil}`); |
| 68 | + retPupil = pupil; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // save record for later checking |
| 74 | + expanded.push({ ...row, Pupil: retPupil }); |
| 75 | + |
| 76 | + return { ...row, Pupil: retPupil }; |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +async function main() { |
| 81 | + config = { |
| 82 | + reception: { |
| 83 | + filename: "CARNIVAL_DISCO-_RECEPTION.csv", |
| 84 | + years: ["Reception"], |
| 85 | + }, |
| 86 | + years1_2_3: { |
| 87 | + filename: "SPRING_DISCO-_YEARS_1_2_3.csv", |
| 88 | + years: ["Year 1", "Year 2", "Year 3"], |
| 89 | + }, |
| 90 | + years4_5_6: { |
| 91 | + filename: "SPRING_DISCO-_YEARS_4_5_6.csv", |
| 92 | + years: ["Year 4", "Year 5", "Year 6"], |
| 93 | + }, |
| 94 | + }; |
| 95 | + |
| 96 | + // filename: "SPRING_DISCO-_YEARS_1__2_and_3-05-02-2025.csv" |
| 97 | + // const years = ["Year 1", "Year 2", "Year 3"]; |
| 98 | + |
| 99 | + async function disco(filename, years) { |
| 100 | + const data = await loadCsv(filename); |
| 101 | + let updated = manipulateData(data); |
| 102 | + updated = expandPupils(updated, years); |
| 103 | + saveCsvToOutFolder(updated, filename); |
| 104 | + console.log("Input Data, Rows: ", data.length); |
| 105 | + console.log("Output Data, Rows: ", updated.length); |
| 106 | + } |
| 107 | + |
| 108 | + disco(config.reception.filename, config.reception.years); |
| 109 | + disco(config.years1_2_3.filename, config.years1_2_3.years); |
| 110 | + disco(config.years4_5_6.filename, config.years4_5_6.years); |
| 111 | +} |
| 112 | + |
| 113 | +main(); |
| 114 | + |
| 115 | +// reception = 48 tickets, outpout 44 |
| 116 | +// years 1,2,3 = 85 tickets, output 85 |
| 117 | +// years 4,5,6 = 114 tickets, output 113 |
0 commit comments