|
| 1 | +const originalData = [ |
| 2 | + { |
| 3 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 4 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 5 | + idn_code: "106425", |
| 6 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 7 | + transaction_date: "2022-11-19T13:44:26+07:00", |
| 8 | + reference: "", |
| 9 | + amount: 4200000, |
| 10 | + transaction_id: "RY7SF5XTXUFW", |
| 11 | + merchant: "Gojek", |
| 12 | + category: "Travel", |
| 13 | + }, |
| 14 | + { |
| 15 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 16 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 17 | + idn_code: "106425", |
| 18 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 19 | + transaction_date: "2022-11-18T12:13:14+07:00", |
| 20 | + reference: "", |
| 21 | + amount: 5500000, |
| 22 | + transaction_id: "BGVXK4DM6AHE", |
| 23 | + merchant: "Gojek", |
| 24 | + category: "Travel", |
| 25 | + }, |
| 26 | + { |
| 27 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 28 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 29 | + idn_code: "106425", |
| 30 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 31 | + transaction_date: "2022-10-18T11:40:09+07:00", |
| 32 | + reference: "", |
| 33 | + amount: 5200000, |
| 34 | + transaction_id: "6CKNDWE3QCYR", |
| 35 | + merchant: "Gojek", |
| 36 | + category: "Travel", |
| 37 | + }, |
| 38 | + { |
| 39 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 40 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 41 | + idn_code: "106425", |
| 42 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 43 | + transaction_date: "2022-10-18T11:37:26+07:00", |
| 44 | + reference: "", |
| 45 | + amount: 6390000, |
| 46 | + transaction_id: "KZFQYWGGW4DP", |
| 47 | + merchant: "Gojek", |
| 48 | + category: "Travel", |
| 49 | + }, |
| 50 | + { |
| 51 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 52 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 53 | + idn_code: "106425", |
| 54 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 55 | + transaction_date: "2022-09-18T11:17:58+07:00", |
| 56 | + reference: "", |
| 57 | + amount: 100000, |
| 58 | + transaction_id: "C49GZVXNZC64", |
| 59 | + merchant: "Gojek", |
| 60 | + category: "Travel", |
| 61 | + }, |
| 62 | + { |
| 63 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 64 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 65 | + idn_code: "106425", |
| 66 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 67 | + transaction_date: "2022-09-18T10:43:48+07:00", |
| 68 | + reference: "", |
| 69 | + amount: 3550000, |
| 70 | + transaction_id: "BSFRH5JXAV5L", |
| 71 | + merchant: "Gojek", |
| 72 | + category: "Travel", |
| 73 | + }, |
| 74 | + { |
| 75 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 76 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 77 | + idn_code: "106425", |
| 78 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 79 | + transaction_date: "2022-10-18T09:45:28+07:00", |
| 80 | + reference: "", |
| 81 | + amount: 3650000, |
| 82 | + transaction_id: "9YWUJK35MQKC", |
| 83 | + merchant: "Gojek", |
| 84 | + category: "Travel", |
| 85 | + }, |
| 86 | + { |
| 87 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 88 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 89 | + idn_code: "106425", |
| 90 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 91 | + transaction_date: "2022-08-18T09:24:25+07:00", |
| 92 | + reference: "", |
| 93 | + amount: 5950000, |
| 94 | + transaction_id: "JGSVHFNUGQ9P", |
| 95 | + merchant: "Gojek", |
| 96 | + category: "Travel", |
| 97 | + }, |
| 98 | + { |
| 99 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 100 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 101 | + idn_code: "106425", |
| 102 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 103 | + transaction_date: "2022-10-18T09:22:41+07:00", |
| 104 | + reference: "", |
| 105 | + amount: 100000, |
| 106 | + transaction_id: "BV72ZXHRPUVH", |
| 107 | + merchant: "Gojek", |
| 108 | + category: "Travel", |
| 109 | + }, |
| 110 | + { |
| 111 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 112 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 113 | + idn_code: "Testing Aspire Debit", |
| 114 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 115 | + transaction_date: "2022-10-17T11:08:12+07:00", |
| 116 | + reference: "", |
| 117 | + amount: 14400000, |
| 118 | + transaction_id: "XLTSRCVZMRVB", |
| 119 | + merchant: "Gojek", |
| 120 | + category: "Travel", |
| 121 | + }, |
| 122 | + { |
| 123 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 124 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 125 | + idn_code: "Testing Aspire Debit", |
| 126 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 127 | + transaction_date: "2022-10-17T10:53:26+07:00", |
| 128 | + reference: "", |
| 129 | + amount: 100000, |
| 130 | + transaction_id: "LCSXZYGP5WWE", |
| 131 | + merchant: "Gojek", |
| 132 | + category: "Travel", |
| 133 | + }, |
| 134 | + { |
| 135 | + school_id: "6bf8539b-2468-44bf-a06c-eb92484a5695", |
| 136 | + card_id: "5829ada0-cdc0-40d0-888e-4c7c503bfa7e", |
| 137 | + idn_code: "106425", |
| 138 | + card_holder_id: "e459bd21-bbb7-4b9a-acc3-09686af37a70", |
| 139 | + transaction_date: "2022-12-17T10:51:18+07:00", |
| 140 | + reference: "", |
| 141 | + amount: 100000, |
| 142 | + transaction_id: "UDEYVB2FCW95", |
| 143 | + merchant: "Gojek", |
| 144 | + category: "Travel", |
| 145 | + }, |
| 146 | +]; |
| 147 | + |
| 148 | +const monthNames = [ |
| 149 | + "Januari", |
| 150 | + "Februari", |
| 151 | + "Maret", |
| 152 | + "April", |
| 153 | + "Mei", |
| 154 | + "Juni", |
| 155 | + "Juli", |
| 156 | + "Agustus", |
| 157 | + "September", |
| 158 | + "Oktober", |
| 159 | + "November", |
| 160 | + "Desember", |
| 161 | +]; |
| 162 | + |
| 163 | +// Helper function, given a JS Date, it will return the name. |
| 164 | +// Example: "November" |
| 165 | +function getMonthName(date) { |
| 166 | + return monthNames[date.getMonth()]; |
| 167 | +} |
| 168 | + |
| 169 | +// Helper function, given a datestring, eg., '2022-10-17T21:07:05+07:00', |
| 170 | +// it will return month and year with format: ["November", "2022"] |
| 171 | +function getMonthAndYear(datestring) { |
| 172 | + const date = new Date(datestring); |
| 173 | + const currYear = date.getFullYear(); |
| 174 | + const currMonth = getMonthName(date); |
| 175 | + return [currMonth, currYear]; |
| 176 | +} |
| 177 | + |
| 178 | +// Helper function to transform Logistic Card transaction. |
| 179 | +// Given array of transactions, it will return a monthly-grouped transactions. |
| 180 | +// Return with shape of : |
| 181 | +// const sample = { |
| 182 | +// "2022": [ |
| 183 | +// { |
| 184 | +// "month": "Januari", |
| 185 | +// "total_amount": 750000, |
| 186 | +// "transactions": [Trx1, Trx2, Trx3, ...] |
| 187 | +// }, |
| 188 | +// { |
| 189 | +// "month": "Februari", |
| 190 | +// "total_amount": 840000, |
| 191 | +// "transactions": [Trx1, Trx2, Trx3, ...] |
| 192 | +// } |
| 193 | +// ] |
| 194 | +// } |
| 195 | +const transformCardTransactions = (input) => { |
| 196 | + if (!Array.isArray(input)) return {}; |
| 197 | + return input |
| 198 | + .sort( |
| 199 | + (a, b) => |
| 200 | + new Date(a.transaction_date).getTime() - |
| 201 | + new Date(b.transaction_date).getTime() |
| 202 | + ) |
| 203 | + .reduce((finalObj, cur) => { |
| 204 | + const [currMonth, currYear] = getMonthAndYear(cur.transaction_date); |
| 205 | + |
| 206 | + // 3. if year and month already exist: |
| 207 | + if (finalObj[currYear] != null) { |
| 208 | + if (finalObj[currYear].findIndex((el) => el.month === currMonth) > -1) { |
| 209 | + const index = finalObj[currYear].findIndex( |
| 210 | + (el) => el.month === currMonth |
| 211 | + ); |
| 212 | + |
| 213 | + finalObj[currYear][index].total_amount += cur.amount; |
| 214 | + finalObj[currYear][index].transactions.push(cur); |
| 215 | + return finalObj; |
| 216 | + } |
| 217 | + |
| 218 | + // 2. if year exist, but new month: |
| 219 | + finalObj[currYear].push({ |
| 220 | + month: currMonth, |
| 221 | + total_amount: cur.amount, |
| 222 | + transactions: [cur], |
| 223 | + }); |
| 224 | + return finalObj; |
| 225 | + } |
| 226 | + |
| 227 | + // 1. if new year and new month: |
| 228 | + finalObj[currYear] = [ |
| 229 | + { |
| 230 | + month: currMonth, |
| 231 | + total_amount: cur.amount, |
| 232 | + transactions: [cur], |
| 233 | + }, |
| 234 | + ]; |
| 235 | + return finalObj; |
| 236 | + }, {}); |
| 237 | +}; |
| 238 | + |
| 239 | +const groupedData = transformCardTransactions(originalData); |
| 240 | + |
| 241 | +console.log(JSON.stringify(groupedData, null, 2)); |
0 commit comments