Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions ghost/core/core/server/services/tiers/tier-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const nql = require('@tryghost/nql');
*/
module.exports = class TierRepository {
/** @type {import('./tier')[]} */
#store = [];
/** @type {Object.<string, true>} */
#ids = {};
#store;
/** @type {Set<string>} */
#ids = new Set();

/** @type {Object} */
#ProductModel;
Expand All @@ -31,16 +31,15 @@ module.exports = class TierRepository {
}

async init() {
this.#store = [];
this.#ids = {};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if init() is called twice (as it is in test fixture utils here), this could leave stale ids in the Set since we're no longer explicitly clearing this.#ids.

Unlikely to cause any real problems I think, but a slight unintended behavioral change.

Adding a this.#ids.clear() at the beginning of init() would solve this, but I'm wouldn't consider this blocking

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Addressed in d8bad23.

const models = await this.#ProductModel.findAll({
withRelated: ['benefits']
});
for (const model of models) {
this.#ids.clear();
this.#store = await Promise.all(models.map(async (model) => {
const tier = await Tier.create(this.mapToTier(model));
this.#store.push(tier);
this.#ids[tier.id.toHexString()] = true;
}
this.#ids.add(tier.id.toHexString());
return tier;
}));
Comment on lines +37 to +42
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Populate the caches atomically after Promise.all() succeeds.

init() now mutates #ids inside the mapped tasks. If one Tier.create() rejects during a later init() call, the method exits with a partially rebuilt #ids set while #store still contains the previous snapshot. That leaves the repository in an inconsistent state and can send save() down the update path for tiers that are no longer in #store.

💡 Suggested change
     async init() {
         const models = await this.#ProductModel.findAll({
             withRelated: ['benefits']
         });
-        this.#ids.clear();
-        this.#store = await Promise.all(models.map(async (model) => {
-            const tier = await Tier.create(this.mapToTier(model));
-            this.#ids.add(tier.id.toHexString());
-            return tier;
-        }));
+        const store = await Promise.all(models.map((model) => {
+            return Tier.create(this.mapToTier(model));
+        }));
+
+        this.#store = store;
+        this.#ids = new Set(store.map(tier => tier.id.toHexString()));
     }
📝 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.

Suggested change
this.#ids.clear();
this.#store = await Promise.all(models.map(async (model) => {
const tier = await Tier.create(this.mapToTier(model));
this.#store.push(tier);
this.#ids[tier.id.toHexString()] = true;
}
this.#ids.add(tier.id.toHexString());
return tier;
}));
const store = await Promise.all(models.map((model) => {
return Tier.create(this.mapToTier(model));
}));
this.#store = store;
this.#ids = new Set(store.map(tier => tier.id.toHexString()));
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ghost/core/core/server/services/tiers/tier-repository.js` around lines 37 -
42, init() mutates this.#ids inside the concurrent map which can leave the
repository half-updated if any Tier.create() rejects; instead, collect results
and ids into local temporaries and only swap them into the instance fields after
Promise.all resolves successfully. Specifically, stop calling this.#ids.add(...)
inside the mapped async tasks; build a local array (e.g., tempStore) from
Promise.all(models.map(...)) and a local Set (e.g., tempIds) from
tempStore.map(t => t.id.toHexString()), then on success assign this.#store =
tempStore and this.#ids = tempIds so the repository state is replaced
atomically. Ensure you still use mapToTier(model) and Tier.create(...) as before
but avoid mutating instance state until all creates succeed.

}

/**
Expand Down Expand Up @@ -136,7 +135,7 @@ module.exports = class TierRepository {

const toSave = await Tier.create(tier);

if (this.#ids[tier.id.toHexString()]) {
if (this.#ids.has(tier.id.toHexString())) {
const existing = this.#store.findIndex((item) => {
return item.id.equals(tier.id);
});
Expand All @@ -147,7 +146,7 @@ module.exports = class TierRepository {
} else {
await this.#ProductModel.add(data);
this.#store.push(toSave);
this.#ids[tier.id.toHexString()] = true;
this.#ids.add(tier.id.toHexString());
}

for (const event of tier.events) {
Expand Down
Loading