Skip to content

Commit

Permalink
Merge pull request #6791 from reactioncommerce/fix/assigning-first-se…
Browse files Browse the repository at this point in the history
…quence

fix: unable to assigning first sequence with sample data
  • Loading branch information
brent-hoover authored Feb 14, 2023
2 parents d71b8ac + 1bbb011 commit 15c2bfc
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ beforeAll(async () => {
shopId: internalShopId
});
await testApp.createUserAndAccount(mockAdminAccount);

await testApp.collections.Sequences.insertOne({
_id: "mockSequenceId",
shopId: internalShopId,
entity: "Promotions",
value: 100000
});
});

// There is no need to delete any test data from collections because
Expand Down
5 changes: 3 additions & 2 deletions packages/api-plugin-sample-data/src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import envalid from "envalid";

const { cleanEnv, bool } = envalid;
const { cleanEnv, bool, json } = envalid;


export default cleanEnv(
Expand All @@ -10,7 +10,8 @@ export default cleanEnv(
default: false,
desc: "Flag to decide whether sample data has to be loaded",
choices: [true, false]
})
}),
SEQUENCE_INITIAL_VALUES: json({ default: { entity: 999 } })
},
{
dotEnvPath: null
Expand Down
30 changes: 30 additions & 0 deletions packages/api-plugin-sample-data/src/loaders/loadSequences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable no-await-in-loop */
import Random from "@reactioncommerce/random";
import config from "../config.js";

const { SEQUENCE_INITIAL_VALUES } = config;

/**
* @summary load Sequences data
* @param {Object} context - The application context
* @param {String} shopId - The Shop ID
* @returns {void}
*/
export default async function loadSequences(context, shopId) {
const { sequenceConfigs, collections: { Sequences } } = context;
if (sequenceConfigs.length === 0) return;

for (const sequence of sequenceConfigs) {
const { entity } = sequence;
const existingSequence = await Sequences.findOne({ shopId, entity });
if (!existingSequence) {
const startingValue = SEQUENCE_INITIAL_VALUES[entity] || 1000000;
await Sequences.insertOne({
_id: Random.id(),
shopId,
entity,
value: startingValue
});
}
}
}
3 changes: 3 additions & 0 deletions packages/api-plugin-sample-data/src/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import loadProducts from "./loaders/loadProducts.js";
import loadNavigation from "./loaders/loadNavigation.js";
import loadShipping from "./loaders/loadShipping.js";
import loadPromotions from "./loaders/loadPromotions.js";
import loadSequences from "./loaders/loadSequences.js";
import config from "./config.js";

/**
Expand Down Expand Up @@ -47,6 +48,8 @@ export default async function loadSampleData(context) {
await loadShipping(context, newShopId);
Logger.info("Loading Promotions");
await loadPromotions(context, newShopId);
Logger.info("Loading Sequences");
await loadSequences(context, newShopId);
Logger.info("Loading Sample Data complete");
return true;
}
1 change: 1 addition & 0 deletions packages/api-plugin-sequences/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@reactioncommerce/api-utils/lib/configs/babel.config.cjs");
1 change: 1 addition & 0 deletions packages/api-plugin-sequences/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@reactioncommerce/api-utils/lib/configs/jest.config.cjs");
7 changes: 7 additions & 0 deletions packages/api-plugin-sequences/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
"@reactioncommerce/babel-remove-es-create-require": "~1.0.0",
"@reactioncommerce/data-factory": "~1.0.1"
},
"scripts": {
"lint": "npm run lint:eslint",
"lint:eslint": "eslint .",
"test": "jest",
"test:watch": "jest --watch",
"test:file": "jest --no-cache --watch --coverage=false"
},
"publishConfig": {
"access": "public"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js";
import incrementSequence from "./incrementSequence.js";

test("incrementSequence returns a correct number", async () => {
mockContext.collections = {
Sequences: {
findOneAndUpdate: jest.fn().mockReturnValueOnce(Promise.resolve({
value: {
value: 1
}
}))
}
};

const result = await incrementSequence(mockContext, "SHOP_ID", "ENTITY");
expect(result).toEqual(1);
});
62 changes: 40 additions & 22 deletions packages/api-plugin-sequences/src/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,58 @@ import config from "./config.js";

const { SEQUENCE_INITIAL_VALUES } = config;

/**
* @summary create new sequence for a shop
* @param {Object} context - The application context
* @param {String} shopId - The shop ID
* @return {Promise<void>} undefined
*/
async function createShopSequence(context, shopId) {
const { sequenceConfigs, collections: { Sequences } } = context;

for (const sequence of sequenceConfigs) {
const { entity } = sequence;
const existingSequence = await Sequences.findOne({ shopId, entity });
if (!existingSequence) {
// eslint-disable-next-line no-await-in-loop
const startingValue = SEQUENCE_INITIAL_VALUES[entity] || 1000000;
Sequences.insertOne({
_id: Random.id(),
shopId,
entity,
value: startingValue
});
}
}
}

/**
* @summary create new sequences if necessary
* @param {Object} context - The application context
* @return {Promise<void>} undefined
*/
export default async function startupSequences(context) {
const { collections: { Shops } } = context;
const session = context.app.mongoClient.startSession();
const { sequenceConfigs, collections: { Sequences: SequenceCollection, Shops } } = context;

const allShops = await Shops.find().toArray();
for (const shop of allShops) {
const { _id: shopId } = shop;
for (const sequence of sequenceConfigs) {
const { entity } = sequence;
try {
await session.withTransaction(async () => {
// eslint-disable-next-line no-await-in-loop
const existingSequence = await SequenceCollection.findOne({ shopId, entity });
if (!existingSequence) {
const startingValue = SEQUENCE_INITIAL_VALUES[entity] || 1000000;
SequenceCollection.insertOne({
_id: Random.id(),
shopId,
entity,
value: startingValue
});
}
});
} catch (error) {
// eslint-disable-next-line no-await-in-loop
await session.endSession();
throw error;
}
try {
await session.withTransaction(async () => {
await createShopSequence(context, shopId);
});
} catch (error) {
// eslint-disable-next-line no-await-in-loop
await session.endSession();
throw error;
}
// eslint-disable-next-line no-await-in-loop
await session.endSession();
}

const { appEvents } = context;
appEvents.on("afterShopCreate", async ({ shop }) => {
await createShopSequence(context, shop._id);
});
}

0 comments on commit 15c2bfc

Please sign in to comment.