const mongoose = require('mongoose'); let invoiceSchema = mongoose.Schema({ invoiceDB: { userID: String, guilds: {}, } }); invoiceSchema.methods.createInvoiceDB = function (userID, guildID) { this.invoiceDB.userID = userID; this.invoiceDB.guilds = {}; this.invoiceDB.guilds[guildID] = { invoices: [] }; }; /* This function is to add a new guild specific invoice list to an already existing invoiceDB document or can be used to reset a data back to default */ async function addInvoiceList(guilds, guildID, userID, client) { let updatedGuilds = guilds; updatedGuilds[guildID] = { invoices: [] }; await client.dbo.collection("invoices").updateOne({"invoiceDB.userID":userID}, {$set: {"invoiceDB.guilds": updatedGuilds}}, function(err, res) { if (err) return false }) return true } async function addInvoice(guildID, userID, invoice, client) { await client.dbo.collection("invoices").updateOne({"invoiceDB.userID": userID}, {$push: {[`invoiceDB.guilds.${guildID}.invoices`]: invoice}}, function(err, res) { if (err) return false }) return true } module.exports = { Invoice: mongoose.model('Invoice', invoiceSchema), addInvoiceList: addInvoiceList, addInvoice: addInvoice, };