Skip to content

Commit 6ebc045

Browse files
committed
fix dnd currency setup with migration
1 parent 8785879 commit 6ebc045

1 file changed

Lines changed: 61 additions & 11 deletions

File tree

source/main.js

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,12 +1849,13 @@ async function handleBankDeposit(interaction) {
18491849
.collection(CONFIG.COLLECTION_NAMES.BANK);
18501850

18511851
const updateResult = await collection.findOneAndUpdate(
1852-
{ channel: interaction.channel.id, currency },
1852+
{ channel: interaction.channel.id, currency, currencySystem: "dnd" },
18531853
{
18541854
$inc: { amount },
18551855
$setOnInsert: {
18561856
channel: interaction.channel.id,
18571857
currency,
1858+
currencySystem: "dnd",
18581859
createdAt: new Date(),
18591860
},
18601861
$set: { updatedAt: new Date() },
@@ -1917,6 +1918,7 @@ async function handleBankWithdraw(interaction) {
19171918
const existingEntry = await collection.findOne({
19181919
channel: interaction.channel.id,
19191920
currency,
1921+
currencySystem: "dnd",
19201922
});
19211923

19221924
if (!existingEntry || existingEntry.amount < amount) {
@@ -1928,7 +1930,7 @@ async function handleBankWithdraw(interaction) {
19281930
}
19291931

19301932
const updateResult = await collection.findOneAndUpdate(
1931-
{ channel: interaction.channel.id, currency },
1933+
{ channel: interaction.channel.id, currency, currencySystem: "dnd" },
19321934
{
19331935
$inc: { amount: -amount },
19341936
$set: { updatedAt: new Date() },
@@ -1954,7 +1956,7 @@ async function handleBankWithdraw(interaction) {
19541956
);
19551957

19561958
if (updateResult.amount <= 0) {
1957-
await collection.deleteOne({ channel: interaction.channel.id, currency });
1959+
await collection.deleteOne({ channel: interaction.channel.id, currency, currencySystem: "dnd" });
19581960
await interaction.editReply(
19591961
`Withdrew \`${amount}\` **${currency}** (${CURRENCY_ABBREVIATIONS[currency]}). Balance: \`${oldAmount}\` → \`0\` ${CURRENCY_ABBREVIATIONS[currency]}. Currency removed from bank.`
19601962
);
@@ -1988,8 +1990,14 @@ async function handleBankBalance(interaction) {
19881990
.db()
19891991
.collection(CONFIG.COLLECTION_NAMES.BANK);
19901992

1993+
// First, update any existing documents that don't have currencySystem field
1994+
await collection.updateMany(
1995+
{ channel: interaction.channel.id, currencySystem: { $exists: false } },
1996+
{ $set: { currencySystem: "dnd" } }
1997+
);
1998+
19911999
const currencies = await collection
1992-
.find({ channel: interaction.channel.id })
2000+
.find({ channel: interaction.channel.id, currencySystem: "dnd" })
19932001
.sort({ currency: 1 })
19942002
.toArray();
19952003

@@ -2084,13 +2092,20 @@ async function handleBankClear(interaction) {
20842092
.db()
20852093
.collection(CONFIG.COLLECTION_NAMES.BANK);
20862094

2095+
// First, update any existing documents that don't have currencySystem field
2096+
await collection.updateMany(
2097+
{ channel: interaction.channel.id, currencySystem: { $exists: false } },
2098+
{ $set: { currencySystem: "dnd" } }
2099+
);
2100+
20872101
// Get currencies before deletion for audit log
20882102
const currenciesBeforeDeletion = await collection
2089-
.find({ channel: interaction.channel.id })
2103+
.find({ channel: interaction.channel.id, currencySystem: "dnd" })
20902104
.toArray();
20912105

20922106
const result = await collection.deleteMany({
20932107
channel: interaction.channel.id,
2108+
currencySystem: "dnd",
20942109
});
20952110

20962111
if (result.deletedCount === 0) {
@@ -2137,8 +2152,14 @@ async function handleBankAudit(interaction) {
21372152
.db()
21382153
.collection(CONFIG.COLLECTION_NAMES.BANK_AUDIT_LOG);
21392154

2155+
// First, update any existing audit documents that don't have currencySystem field
2156+
await auditCollection.updateMany(
2157+
{ channel: interaction.channel.id, currencySystem: { $exists: false } },
2158+
{ $set: { currencySystem: "dnd" } }
2159+
);
2160+
21402161
const auditEntries = await auditCollection
2141-
.find({ channel: interaction.channel.id })
2162+
.find({ channel: interaction.channel.id, $or: [{ currencySystem: "dnd" }, { currencySystem: { $exists: false } }] })
21422163
.sort({ timestamp: -1 })
21432164
.limit(limit)
21442165
.toArray();
@@ -2203,18 +2224,31 @@ async function handleBankConvert(interaction) {
22032224
const bankCollection = client.db().collection(CONFIG.COLLECTION_NAMES.BANK);
22042225
const feeCollection = client.db().collection(CONFIG.COLLECTION_NAMES.BANK_FEES);
22052226

2227+
// First, update any existing fee documents that don't have currencySystem field
2228+
await feeCollection.updateMany(
2229+
{ channel: interaction.channel.id, currencySystem: { $exists: false } },
2230+
{ $set: { currencySystem: "dnd" } }
2231+
);
2232+
22062233
// Get fee rate for this channel first
2207-
const feeConfig = await feeCollection.findOne({ channel: interaction.channel.id });
2234+
const feeConfig = await feeCollection.findOne({ channel: interaction.channel.id, currencySystem: "dnd" });
22082235
const feeRate = feeConfig?.feeRate || DEFAULT_CONVERSION_FEE;
22092236

22102237
// Calculate fee from source currency first
22112238
const feeAmount = Math.ceil(amount * feeRate);
22122239
const totalRequired = amount + feeAmount;
22132240

2241+
// First, update any existing documents that don't have currencySystem field
2242+
await bankCollection.updateMany(
2243+
{ channel: interaction.channel.id, currencySystem: { $exists: false } },
2244+
{ $set: { currencySystem: "dnd" } }
2245+
);
2246+
22142247
// Check if user has sufficient balance (including fee)
22152248
const sourceEntry = await bankCollection.findOne({
22162249
channel: interaction.channel.id,
22172250
currency: fromCurrency,
2251+
currencySystem: "dnd",
22182252
});
22192253

22202254
if (!sourceEntry || sourceEntry.amount < totalRequired) {
@@ -2258,18 +2292,19 @@ async function handleBankConvert(interaction) {
22582292
// Perform the conversion
22592293
// Remove source currency (conversion amount + fee)
22602294
await bankCollection.updateOne(
2261-
{ channel: interaction.channel.id, currency: fromCurrency },
2295+
{ channel: interaction.channel.id, currency: fromCurrency, currencySystem: "dnd" },
22622296
{ $inc: { amount: -totalRequired }, $set: { updatedAt: new Date() } }
22632297
);
22642298

22652299
// Add target currency
22662300
await bankCollection.findOneAndUpdate(
2267-
{ channel: interaction.channel.id, currency: toCurrency },
2301+
{ channel: interaction.channel.id, currency: toCurrency, currencySystem: "dnd" },
22682302
{
22692303
$inc: { amount: finalConvertedAmount },
22702304
$setOnInsert: {
22712305
channel: interaction.channel.id,
22722306
currency: toCurrency,
2307+
currencySystem: "dnd",
22732308
createdAt: new Date(),
22742309
},
22752310
$set: { updatedAt: new Date() },
@@ -2280,6 +2315,7 @@ async function handleBankConvert(interaction) {
22802315
// Clean up zero balances
22812316
await bankCollection.deleteMany({
22822317
channel: interaction.channel.id,
2318+
currencySystem: "dnd",
22832319
amount: { $lte: 0 }
22842320
});
22852321

@@ -2341,8 +2377,14 @@ async function handleBankSetFee(interaction) {
23412377
await withDatabase(async (client) => {
23422378
const feeCollection = client.db().collection(CONFIG.COLLECTION_NAMES.BANK_FEES);
23432379

2380+
// First, update any existing fee documents that don't have currencySystem field
2381+
await feeCollection.updateMany(
2382+
{ channel: interaction.channel.id, currencySystem: { $exists: false } },
2383+
{ $set: { currencySystem: "dnd" } }
2384+
);
2385+
23442386
await feeCollection.findOneAndUpdate(
2345-
{ channel: interaction.channel.id },
2387+
{ channel: interaction.channel.id, currencySystem: "dnd" },
23462388
{
23472389
$set: {
23482390
feeRate,
@@ -2351,6 +2393,7 @@ async function handleBankSetFee(interaction) {
23512393
},
23522394
$setOnInsert: {
23532395
channel: interaction.channel.id,
2396+
currencySystem: "dnd",
23542397
createdAt: new Date(),
23552398
},
23562399
},
@@ -2395,7 +2438,13 @@ async function handleBankFees(interaction) {
23952438
await withDatabase(async (client) => {
23962439
const feeCollection = client.db().collection(CONFIG.COLLECTION_NAMES.BANK_FEES);
23972440

2398-
const feeConfig = await feeCollection.findOne({ channel: interaction.channel.id });
2441+
// First, update any existing fee documents that don't have currencySystem field
2442+
await feeCollection.updateMany(
2443+
{ channel: interaction.channel.id, currencySystem: { $exists: false } },
2444+
{ $set: { currencySystem: "dnd" } }
2445+
);
2446+
2447+
const feeConfig = await feeCollection.findOne({ channel: interaction.channel.id, currencySystem: "dnd" });
23992448
const feeRate = feeConfig?.feeRate || DEFAULT_CONVERSION_FEE;
24002449
const feePercentage = (feeRate * 100).toFixed(1);
24012450

@@ -2444,6 +2493,7 @@ async function recordBankAuditLog(client, channel, action, userId, username, det
24442493
action,
24452494
userId,
24462495
username,
2496+
currencySystem: "dnd",
24472497
timestamp: new Date(),
24482498
details,
24492499
};

0 commit comments

Comments
 (0)