Created
March 26, 2021 07:57
-
-
Save roosnic1/54720dc74d4dfe6e3019a7a68dbdc258 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const filePath = path.join(__dirname, '../users.csv') | |
fs.readFile(filePath, async (error, data) => { | |
if (error) { | |
return console.log('error reading file', error) | |
} | |
const users = await neatCsv(data) | |
const periodsDb = await csvdb(`${__dirname}/../periods.csv`) | |
const lineDb = await csvdb(`${__dirname}/../lineitems.csv`) | |
for (const user of users) { | |
console.log(`Importing User ${user.name} with ID ${user.id}`) | |
if (!user.subscription_id) { | |
console.warn(`User ${user.id} does not have a subscription`) | |
continue | |
} | |
const periodsLine = [] | |
const periods = await periodsDb.find({subscription_id: user.subscription_id}) | |
for (const period of periods) { | |
const lineItem = await lineDb.find({id: period.line_item_id}) | |
const dateStart = new Date(period.starts_on) | |
const dateEnd = new Date(period.ends_on) | |
const difference = dateEnd.getTime() - dateStart.getTime() | |
const differenceInDays = difference / (1000 * 3600 * 24) | |
let periodicity = '' | |
if (differenceInDays < 80) { | |
periodicity = PaymentPeriodicity.Monthly | |
} else if (differenceInDays < 300) { | |
periodicity = PaymentPeriodicity.Quarterly | |
} else { | |
periodicity = PaymentPeriodicity.Yearly | |
} | |
periodsLine.push({ | |
...period, | |
periodicity, | |
lineItem | |
}) | |
} | |
const newUser = await dbAdapter.user.createUser({ | |
input: { | |
email: user.email, | |
name: user.full_name, | |
roleIDs: [] | |
}, | |
password: 'randomTestPasswor' | |
}) | |
if (!newUser) { | |
console.log('Could not create user', user) | |
continue | |
} | |
let paymentPeriodicity: PaymentPeriodicity = PaymentPeriodicity.Monthly | |
let monthlyAmount = 5 | |
switch (user.subscription_periodicity) { | |
case 'jährlich': | |
paymentPeriodicity = PaymentPeriodicity.Yearly | |
monthlyAmount = Math.floor(parseInt(user.subscription_amount) / 12) | |
break | |
case 'vierteljährlich': | |
paymentPeriodicity = PaymentPeriodicity.Quarterly | |
monthlyAmount = Math.floor(parseInt(user.subscription_amount) / 3) | |
break | |
case 'monatlich': | |
paymentPeriodicity = PaymentPeriodicity.Monthly | |
monthlyAmount = parseInt(user.subscription_amount) * 100 | |
} | |
const subscription = await dbAdapter.user.updateUserSubscription({ | |
userID: newUser.id, | |
input: { | |
paidUntil: user.subscription_paid_until | |
? new Date(user.subscription_paid_until) | |
: null, | |
autoRenew: user.subscription_renew_automatically === '1', | |
memberPlanID: 'WKagRZ6jd9YVtZM5', | |
deactivatedAt: | |
user.subscription_ends_on === '-' ? null : new Date(user.subscription_ends_on), | |
paymentPeriodicity, | |
monthlyAmount, | |
paymentMethodID: 'knl3FR2p2LxJXtIo', | |
startsAt: new Date(user.subscription_starts_on) | |
} | |
}) | |
for (const period of periodsLine) { | |
const invoice = await dbAdapter.invoice.createInvoice({ | |
input: { | |
dueAt: period?.lineItem?.created_at | |
? new Date(period?.lineItem?.created_at) | |
: new Date(), | |
paidAt: period?.lineItem?.created_at | |
? new Date(period?.lineItem?.created_at) | |
: new Date(), | |
items: [ | |
{ | |
name: period?.lineItem?.title ?? 'N/A', | |
quantity: 1, | |
amount: parseInt(period?.lineItem?.amount), | |
createdAt: new Date(period?.lineItem?.created_at), | |
modifiedAt: new Date(period?.lineItem?.created_at) | |
} | |
], | |
mail: newUser.email, | |
userID: newUser.id, | |
canceledAt: null | |
} | |
}) | |
await dbAdapter.user.addUserSubscriptionPeriod({ | |
userID: newUser.id, | |
input: { | |
invoiceID: invoice.id, | |
startsAt: new Date(period.starts_on), | |
endsAt: new Date(period.ends_on), | |
paymentPeriodicity: period.periodicity as PaymentPeriodicity, | |
amount: parseInt(period.lineItem?.amount) | |
} | |
}) | |
} | |
console.log('subscription', subscription) | |
} | |
process.exit(0) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment