fix: track FreeFavorsEarned & FreeFavorsUsed #792

Merged
Sainan merged 2 commits from track-favors into main 2025-01-17 05:43:51 -08:00
Showing only changes of commit cb9b5222f4 - Show all commits

View File

@ -79,23 +79,33 @@ export const handlePurchase = async (
switch (purchaseRequest.PurchaseParams.Source) { switch (purchaseRequest.PurchaseParams.Source) {
case 2: case 2:
if (!purchaseRequest.PurchaseParams.UseFreeFavor!) { {
const syndicateTag = purchaseRequest.PurchaseParams.SyndicateTag!; const syndicateTag = purchaseRequest.PurchaseParams.SyndicateTag!;
const syndicate = ExportSyndicates[syndicateTag]; if (purchaseRequest.PurchaseParams.UseFreeFavor!) {
if (syndicate) { const inventory = await getInventory(accountId);
const favour = syndicate.favours.find(x => x.storeItem == purchaseRequest.PurchaseParams.StoreItem); const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag)!;
if (favour) { affiliation.FreeFavorsUsed ??= [];
const inventory = await getInventory(accountId); affiliation.FreeFavorsUsed.push(affiliation.FreeFavorsEarned![affiliation.FreeFavorsUsed.length]);
const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag); await inventory.save();
if (affiliation) { } else {
purchaseResponse.Standing = [ const syndicate = ExportSyndicates[syndicateTag];
{ if (syndicate) {
Tag: syndicateTag, const favour = syndicate.favours.find(
Standing: favour.standingCost x => x.storeItem == purchaseRequest.PurchaseParams.StoreItem
} );
]; if (favour) {
affiliation.Standing -= favour.standingCost; const inventory = await getInventory(accountId);
await inventory.save(); const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag);
if (affiliation) {
purchaseResponse.Standing = [
{
Tag: syndicateTag,
Standing: favour.standingCost
}
];
coderabbitai[bot] commented 2025-01-16 21:43:12 -08:00 (Migrated from github.com)
Review

⚠️ Potential issue

Fix potential array index out of bounds and add validation

The current implementation has several critical issues:

  1. No validation that FreeFavorsEarned exists
  2. Potential array index out of bounds when accessing FreeFavorsEarned
  3. No check if there are available favors to use

Apply this diff to fix these issues:

     if (purchaseRequest.PurchaseParams.UseFreeFavor!) {
         const inventory = await getInventory(accountId);
         const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag)!;
+        if (!affiliation.FreeFavorsEarned?.length) {
+            throw new Error("No free favors available");
+        }
         affiliation.FreeFavorsUsed ??= [];
-        affiliation.FreeFavorsUsed.push(affiliation.FreeFavorsEarned![affiliation.FreeFavorsUsed.length]);
+        const availableFavors = affiliation.FreeFavorsEarned.length - (affiliation.FreeFavorsUsed?.length || 0);
+        if (availableFavors <= 0) {
+            throw new Error("All free favors have been used");
+        }
+        affiliation.FreeFavorsUsed.push(affiliation.FreeFavorsEarned[affiliation.FreeFavorsUsed.length]);
         await inventory.save();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

                if (purchaseRequest.PurchaseParams.UseFreeFavor!) {
                    const inventory = await getInventory(accountId);
                    const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag)!;
                    if (!affiliation.FreeFavorsEarned?.length) {
                        throw new Error("No free favors available");
                    }
                    affiliation.FreeFavorsUsed ??= [];
                    const availableFavors = affiliation.FreeFavorsEarned.length - (affiliation.FreeFavorsUsed?.length || 0);
                    if (availableFavors <= 0) {
                        throw new Error("All free favors have been used");
                    }
                    affiliation.FreeFavorsUsed.push(affiliation.FreeFavorsEarned[affiliation.FreeFavorsUsed.length]);
                    await inventory.save();
_:warning: Potential issue_ **Fix potential array index out of bounds and add validation** The current implementation has several critical issues: 1. No validation that `FreeFavorsEarned` exists 2. Potential array index out of bounds when accessing `FreeFavorsEarned` 3. No check if there are available favors to use Apply this diff to fix these issues: ```diff if (purchaseRequest.PurchaseParams.UseFreeFavor!) { const inventory = await getInventory(accountId); const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag)!; + if (!affiliation.FreeFavorsEarned?.length) { + throw new Error("No free favors available"); + } affiliation.FreeFavorsUsed ??= []; - affiliation.FreeFavorsUsed.push(affiliation.FreeFavorsEarned![affiliation.FreeFavorsUsed.length]); + const availableFavors = affiliation.FreeFavorsEarned.length - (affiliation.FreeFavorsUsed?.length || 0); + if (availableFavors <= 0) { + throw new Error("All free favors have been used"); + } + affiliation.FreeFavorsUsed.push(affiliation.FreeFavorsEarned[affiliation.FreeFavorsUsed.length]); await inventory.save(); ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. `````suggestion if (purchaseRequest.PurchaseParams.UseFreeFavor!) { const inventory = await getInventory(accountId); const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag)!; if (!affiliation.FreeFavorsEarned?.length) { throw new Error("No free favors available"); } affiliation.FreeFavorsUsed ??= []; const availableFavors = affiliation.FreeFavorsEarned.length - (affiliation.FreeFavorsUsed?.length || 0); if (availableFavors <= 0) { throw new Error("All free favors have been used"); } affiliation.FreeFavorsUsed.push(affiliation.FreeFavorsEarned[affiliation.FreeFavorsUsed.length]); await inventory.save(); ````` </details> <!-- suggestion_end --> <!-- This is an auto-generated comment by CodeRabbit -->
affiliation.Standing -= favour.standingCost;
await inventory.save();
}
} }
} }
} }