From 79eac5e87c24b620044d1dabebea1c7cdd30aecf Mon Sep 17 00:00:00 2001 From: Sujith Date: Fri, 4 Nov 2022 13:51:06 +0530 Subject: [PATCH 1/8] feat: data migration for fulfillment feature Signed-off-by: Sujith --- packages/api-plugin-fulfillment/index.js | 2 + .../api-plugin-fulfillment/migrations/2.js | 45 +++++++++++++++ .../api-plugin-fulfillment/migrations/3.js | 56 +++++++++++++++++++ .../api-plugin-fulfillment/migrations/4.js | 43 ++++++++++++++ .../migrations/index.js | 17 ++++++ .../migrations/migrationsNamespace.js | 1 + packages/api-plugin-fulfillment/package.json | 1 + .../api-plugin-fulfillment/src/preStartup.js | 33 +++++++++++ 8 files changed, 198 insertions(+) create mode 100644 packages/api-plugin-fulfillment/migrations/2.js create mode 100644 packages/api-plugin-fulfillment/migrations/3.js create mode 100644 packages/api-plugin-fulfillment/migrations/4.js create mode 100644 packages/api-plugin-fulfillment/migrations/index.js create mode 100644 packages/api-plugin-fulfillment/migrations/migrationsNamespace.js diff --git a/packages/api-plugin-fulfillment/index.js b/packages/api-plugin-fulfillment/index.js index d7ea8b28c59..ff1789c8e87 100644 --- a/packages/api-plugin-fulfillment/index.js +++ b/packages/api-plugin-fulfillment/index.js @@ -1,3 +1,5 @@ import register from "./src/index.js"; +export { default as migrations } from "./migrations/index.js"; + export default register; diff --git a/packages/api-plugin-fulfillment/migrations/2.js b/packages/api-plugin-fulfillment/migrations/2.js new file mode 100644 index 00000000000..a1259d1ce57 --- /dev/null +++ b/packages/api-plugin-fulfillment/migrations/2.js @@ -0,0 +1,45 @@ +const COLL_FF_SOURCE = "Shipments"; +const COLL_FF_DEST = "Fulfillment"; +const COLL_FFR_SOURCE = "FlatRateFulfillmentRestrictions"; +const COLL_FFR_DEST = "FulfillmentRestrictions"; + +/** + * @summary migrates the database down one version + * @param {Object} context Migration context + * @param {Object} context.db MongoDB `Db` instance + * @param {Function} context.progress A function to report progress, takes percent + * number as argument. + * @return {undefined} + */ +async function down({ db, progress }) { + progress(0); + + await db.collections(COLL_FF_DEST).drop(); + progress(50); + + await db.collections(COLL_FFR_DEST).drop(); + progress(100); +} + +/** + * @summary Performs migration up from previous data version + * @param {Object} context Migration context + * @param {Object} context.db MongoDB `Db` instance + * @param {Function} context.progress A function to report progress, takes percent + * number as argument. + * @return {undefined} + */ +async function up({ db, progress }) { + progress(0); + + await db.collections(COLL_FF_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FF_DEST }]); + progress(50); + + await db.collections(COLL_FFR_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FFR_DEST }]); + progress(100); +} + +export default { + down, + up +}; diff --git a/packages/api-plugin-fulfillment/migrations/3.js b/packages/api-plugin-fulfillment/migrations/3.js new file mode 100644 index 00000000000..1becbedaf41 --- /dev/null +++ b/packages/api-plugin-fulfillment/migrations/3.js @@ -0,0 +1,56 @@ +const FF_TYPE = "shipping"; +const FF_METHOD = "flatRate"; +const COLL_DEST = "Fulfillment"; + +/** + * @summary Performs migration up from previous data version + * @param {Object} context Migration context + * @param {Object} context.db MongoDB `Db` instance + * @param {Function} context.progress A function to report progress, takes percent + * number as argument. + * @return {undefined} + */ +async function up({ db, progress }) { + progress(0); + + const operations = []; + + const ffTypeUpdate = { + updateMany: { + filter: { fulfillmentType: { $exists: false } }, + update: { + $set: { + fulfillmentType: FF_TYPE + } + } + } + }; + + const ffMethodUpdate = { + updateMany: { + filter: { methods: { $exists: true } }, + update: { + $set: { + "methods.$[eachMethod].fulfillmentMethod": FF_METHOD + } + }, + arrayFilters: [ + { + "eachMethod.fulfillmentMethod": { $exists: false } + } + ] + } + }; + + operations.push(ffTypeUpdate); + operations.push(ffMethodUpdate); + + await db.collections(COLL_DEST).bulkWrite(operations); + + progress(100); +} + +export default { + down: "impossible", // We are not tracking the updated documents, hence cannot revert + up +}; diff --git a/packages/api-plugin-fulfillment/migrations/4.js b/packages/api-plugin-fulfillment/migrations/4.js new file mode 100644 index 00000000000..48534ccd3c2 --- /dev/null +++ b/packages/api-plugin-fulfillment/migrations/4.js @@ -0,0 +1,43 @@ +/** + * @summary Performs migration up from previous data version + * @param {Object} context Migration context + * @param {Object} context.db MongoDB `Db` instance + * @param {Function} context.progress A function to report progress, takes percent + * number as argument. + * @return {undefined} + */ +async function up({ db, progress }) { + progress(0); + const affectedGroups = [ + "owner", + "shop manager" + ]; + + const newShopPermissions = [ + "reaction:legacy:fulfillmentRestrictions/create", + "reaction:legacy:fulfillmentRestrictions/delete", + "reaction:legacy:fulfillmentRestrictions/read", + "reaction:legacy:fulfillmentRestrictions/update", + "reaction:legacy:fulfillmentTypes/create", + "reaction:legacy:fulfillmentTypes/delete", + "reaction:legacy:fulfillmentTypes/read", + "reaction:legacy:fulfillmentTypes/update", + "reaction:legacy:fulfillmentMethods/create", + "reaction:legacy:fulfillmentMethods/delete", + "reaction:legacy:fulfillmentMethods/read", + "reaction:legacy:fulfillmentMethods/update" + ]; + + await db.collection("Groups").updateMany({ + slug: { $in: affectedGroups } + }, { + $addToSet: { permissions: { $each: newShopPermissions } } + }); + + progress(100); +} + +export default { + down: "impossible", + up +}; diff --git a/packages/api-plugin-fulfillment/migrations/index.js b/packages/api-plugin-fulfillment/migrations/index.js new file mode 100644 index 00000000000..783e2bf9115 --- /dev/null +++ b/packages/api-plugin-fulfillment/migrations/index.js @@ -0,0 +1,17 @@ +import { migrationsNamespace } from "./migrationsNamespace.js"; +import migration2 from "./2.js"; +import migration3 from "./3.js"; +import migration4 from "./4.js"; + +export default { + tracks: [ + { + namespace: migrationsNamespace, + migrations: { + 2: migration2, + 3: migration3, + 4: migration4 + } + } + ] +}; diff --git a/packages/api-plugin-fulfillment/migrations/migrationsNamespace.js b/packages/api-plugin-fulfillment/migrations/migrationsNamespace.js new file mode 100644 index 00000000000..49c2b68e1ce --- /dev/null +++ b/packages/api-plugin-fulfillment/migrations/migrationsNamespace.js @@ -0,0 +1 @@ +export const migrationsNamespace = "fulfillment"; diff --git a/packages/api-plugin-fulfillment/package.json b/packages/api-plugin-fulfillment/package.json index 5497612a419..342f5c67bf2 100644 --- a/packages/api-plugin-fulfillment/package.json +++ b/packages/api-plugin-fulfillment/package.json @@ -32,6 +32,7 @@ "@reactioncommerce/api-utils": "^1.16.9", "@reactioncommerce/reaction-error": "^1.0.1", "@reactioncommerce/random": "^1.0.2", + "@reactioncommerce/db-version-check": "^1.0.0", "lodash": "^4.17.21", "simpl-schema": "^1.12.2" }, diff --git a/packages/api-plugin-fulfillment/src/preStartup.js b/packages/api-plugin-fulfillment/src/preStartup.js index e65d3743063..6db7d24c595 100644 --- a/packages/api-plugin-fulfillment/src/preStartup.js +++ b/packages/api-plugin-fulfillment/src/preStartup.js @@ -1,8 +1,40 @@ import Logger from "@reactioncommerce/logger"; import ReactionError from "@reactioncommerce/reaction-error"; +import doesDatabaseVersionMatch from "@reactioncommerce/db-version-check"; +import { migrationsNamespace } from "../migrations/migrationsNamespace.js"; import { extendFulfillmentSchemas } from "./simpleSchemas.js"; const logCtx = { name: "fulfillment", file: "preStartup" }; +const expectedVersion = 4; + +/** + * @summary Checks if the version of the database matches requirement + * @param {Object} context Startup context + * @returns {undefined} + */ +async function dbVersionCheck(context) { + const setToExpectedIfMissing = async () => { + const anyFulfillment = await context.collections.Fulfillment.findOne(); + const anyRestriction = await context.collections.FulfillmentRestrictions.findOne(); + + return !anyFulfillment || !anyRestriction; + }; + + const ok = await doesDatabaseVersionMatch({ + // `db` is a Db instance from the `mongodb` NPM package, + // such as what is returned when you do `client.db()` + db: context.app.db, + // These must match one of the namespaces and versions + // your package exports in the `migrations` named export + expectedVersion, + namespace: migrationsNamespace, + setToExpectedIfMissing + }); + + if (!ok) { + throw new Error(`Database needs migrating. The "${migrationsNamespace}" namespace must be at version ${expectedVersion}.`); + } +} /** * @summary Called before startup to extend schemas @@ -10,6 +42,7 @@ const logCtx = { name: "fulfillment", file: "preStartup" }; * @returns {undefined} */ export default async function fulfillmentPreStartup(context) { + await dbVersionCheck(context); const allFulfillmentTypesArray = context.fulfillment?.registeredFulfillmentTypes; if (!allFulfillmentTypesArray || allFulfillmentTypesArray.length === 0) { From c5ab64e757b4cb461953a21eec97181fa8069843 Mon Sep 17 00:00:00 2001 From: Sujith Date: Tue, 4 Oct 2022 16:48:07 +0530 Subject: [PATCH 2/8] feat: migrate shopsetting for default ff-type Signed-off-by: Sujith --- packages/api-plugin-fulfillment/migrations/4.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/api-plugin-fulfillment/migrations/4.js b/packages/api-plugin-fulfillment/migrations/4.js index 48534ccd3c2..9a589926ad9 100644 --- a/packages/api-plugin-fulfillment/migrations/4.js +++ b/packages/api-plugin-fulfillment/migrations/4.js @@ -14,6 +14,7 @@ async function up({ db, progress }) { ]; const newShopPermissions = [ + "reaction:legacy:fulfillmentTypes/update:settings", "reaction:legacy:fulfillmentRestrictions/create", "reaction:legacy:fulfillmentRestrictions/delete", "reaction:legacy:fulfillmentRestrictions/read", From a6356cbbd3042eaf5825f65b20e9dd1b6996c8fd Mon Sep 17 00:00:00 2001 From: Sujith Date: Thu, 6 Oct 2022 17:39:01 +0530 Subject: [PATCH 3/8] fix: migration script fix Signed-off-by: Sujith --- packages/api-plugin-fulfillment/migrations/2.js | 8 +++++--- packages/api-plugin-fulfillment/migrations/3.js | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/api-plugin-fulfillment/migrations/2.js b/packages/api-plugin-fulfillment/migrations/2.js index a1259d1ce57..0ea010f9449 100644 --- a/packages/api-plugin-fulfillment/migrations/2.js +++ b/packages/api-plugin-fulfillment/migrations/2.js @@ -1,4 +1,4 @@ -const COLL_FF_SOURCE = "Shipments"; +const COLL_FF_SOURCE = "Shipping"; const COLL_FF_DEST = "Fulfillment"; const COLL_FFR_SOURCE = "FlatRateFulfillmentRestrictions"; const COLL_FFR_DEST = "FulfillmentRestrictions"; @@ -32,10 +32,12 @@ async function down({ db, progress }) { async function up({ db, progress }) { progress(0); - await db.collections(COLL_FF_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FF_DEST }]); + const shippingCopyResp = await db.collection(COLL_FF_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FF_DEST }]).next(); + if (shippingCopyResp) throw new Error("Error in copying Shipping collection"); // above command returns null if successful progress(50); - await db.collections(COLL_FFR_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FFR_DEST }]); + const flatRateRestCopyResp = await db.collection(COLL_FFR_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FFR_DEST }]).next(); + if (flatRateRestCopyResp) throw new Error("Error in copying FlatRateFulfillmentRestrictions collection"); // above command returns null if successful progress(100); } diff --git a/packages/api-plugin-fulfillment/migrations/3.js b/packages/api-plugin-fulfillment/migrations/3.js index 1becbedaf41..770899cc5dc 100644 --- a/packages/api-plugin-fulfillment/migrations/3.js +++ b/packages/api-plugin-fulfillment/migrations/3.js @@ -45,8 +45,8 @@ async function up({ db, progress }) { operations.push(ffTypeUpdate); operations.push(ffMethodUpdate); - await db.collections(COLL_DEST).bulkWrite(operations); - + const bulkWriteResp = await db.collection(COLL_DEST).bulkWrite(operations); + if (bulkWriteResp.writeErrors && bulkWriteResp.writeErrors.length) throw new Error("Error while updating Fulfillment collection"); progress(100); } From 3d016d77d4cf0f7e8966190e58bc8d3923fc9a3c Mon Sep 17 00:00:00 2001 From: Sujith Date: Thu, 13 Oct 2022 11:08:19 +0530 Subject: [PATCH 4/8] fix: review comment fixes Signed-off-by: Sujith --- .../api-plugin-fulfillment/migrations/3.js | 2 +- .../api-plugin-fulfillment/migrations/4.js | 66 ++++++++++++------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/packages/api-plugin-fulfillment/migrations/3.js b/packages/api-plugin-fulfillment/migrations/3.js index 770899cc5dc..e1b1506034e 100644 --- a/packages/api-plugin-fulfillment/migrations/3.js +++ b/packages/api-plugin-fulfillment/migrations/3.js @@ -51,6 +51,6 @@ async function up({ db, progress }) { } export default { - down: "impossible", // We are not tracking the updated documents, hence cannot revert + down: "unnecessary", // The newly created collections would get dropped in stage2 up }; diff --git a/packages/api-plugin-fulfillment/migrations/4.js b/packages/api-plugin-fulfillment/migrations/4.js index 9a589926ad9..af2dda9bbdf 100644 --- a/packages/api-plugin-fulfillment/migrations/4.js +++ b/packages/api-plugin-fulfillment/migrations/4.js @@ -1,3 +1,43 @@ +const affectedGlobalGroups = [ + "owner", + "shop manager" +]; + +const newGlobalPermissions = [ + "reaction:legacy:fulfillmentRestrictions/create", + "reaction:legacy:fulfillmentRestrictions/delete", + "reaction:legacy:fulfillmentRestrictions/read", + "reaction:legacy:fulfillmentRestrictions/update", + "reaction:legacy:fulfillmentTypes/create", + "reaction:legacy:fulfillmentTypes/delete", + "reaction:legacy:fulfillmentTypes/read", + "reaction:legacy:fulfillmentTypes/update", + "reaction:legacy:fulfillmentMethods/create", + "reaction:legacy:fulfillmentMethods/delete", + "reaction:legacy:fulfillmentMethods/read", + "reaction:legacy:fulfillmentMethods/update" +]; + +/** + * @summary migrates the database down one version + * @param {Object} context Migration context + * @param {Object} context.db MongoDB `Db` instance + * @param {Function} context.progress A function to report progress, takes percent + * number as argument. + * @return {undefined} + */ +async function down({ db, progress }) { + progress(0); + + await db.collection("Groups").updateMany({ + slug: { $in: affectedGlobalGroups } + }, { + $pullAll: { permissions: newGlobalPermissions } + }); + + progress(100); +} + /** * @summary Performs migration up from previous data version * @param {Object} context Migration context @@ -8,37 +48,17 @@ */ async function up({ db, progress }) { progress(0); - const affectedGroups = [ - "owner", - "shop manager" - ]; - - const newShopPermissions = [ - "reaction:legacy:fulfillmentTypes/update:settings", - "reaction:legacy:fulfillmentRestrictions/create", - "reaction:legacy:fulfillmentRestrictions/delete", - "reaction:legacy:fulfillmentRestrictions/read", - "reaction:legacy:fulfillmentRestrictions/update", - "reaction:legacy:fulfillmentTypes/create", - "reaction:legacy:fulfillmentTypes/delete", - "reaction:legacy:fulfillmentTypes/read", - "reaction:legacy:fulfillmentTypes/update", - "reaction:legacy:fulfillmentMethods/create", - "reaction:legacy:fulfillmentMethods/delete", - "reaction:legacy:fulfillmentMethods/read", - "reaction:legacy:fulfillmentMethods/update" - ]; await db.collection("Groups").updateMany({ - slug: { $in: affectedGroups } + slug: { $in: affectedGlobalGroups } }, { - $addToSet: { permissions: { $each: newShopPermissions } } + $addToSet: { permissions: { $each: newGlobalPermissions } } }); progress(100); } export default { - down: "impossible", + down, up }; From 3acaeb45017ca5fb11b865d312da4333f892a451 Mon Sep 17 00:00:00 2001 From: Sujith Date: Thu, 13 Oct 2022 18:17:44 +0530 Subject: [PATCH 5/8] fix: typo in 2.js Signed-off-by: Sujith --- packages/api-plugin-fulfillment/migrations/2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api-plugin-fulfillment/migrations/2.js b/packages/api-plugin-fulfillment/migrations/2.js index 0ea010f9449..17160fc81fc 100644 --- a/packages/api-plugin-fulfillment/migrations/2.js +++ b/packages/api-plugin-fulfillment/migrations/2.js @@ -14,10 +14,10 @@ const COLL_FFR_DEST = "FulfillmentRestrictions"; async function down({ db, progress }) { progress(0); - await db.collections(COLL_FF_DEST).drop(); + await db.collection(COLL_FF_DEST).drop(); progress(50); - await db.collections(COLL_FFR_DEST).drop(); + await db.collection(COLL_FFR_DEST).drop(); progress(100); } From bc2974d5c40d9560e2dfd81fdfdf83c72ddf7ad8 Mon Sep 17 00:00:00 2001 From: Sujith Date: Mon, 7 Nov 2022 18:57:26 +0530 Subject: [PATCH 6/8] fix: review comment fixes2 Signed-off-by: Sujith --- .../api-plugin-fulfillment/migrations/2.js | 132 +++++++++++++++++- .../api-plugin-fulfillment/migrations/3.js | 56 -------- .../api-plugin-fulfillment/migrations/4.js | 64 --------- .../migrations/index.js | 6 +- 4 files changed, 132 insertions(+), 126 deletions(-) delete mode 100644 packages/api-plugin-fulfillment/migrations/3.js delete mode 100644 packages/api-plugin-fulfillment/migrations/4.js diff --git a/packages/api-plugin-fulfillment/migrations/2.js b/packages/api-plugin-fulfillment/migrations/2.js index 17160fc81fc..a08b09959c0 100644 --- a/packages/api-plugin-fulfillment/migrations/2.js +++ b/packages/api-plugin-fulfillment/migrations/2.js @@ -2,6 +2,25 @@ const COLL_FF_SOURCE = "Shipping"; const COLL_FF_DEST = "Fulfillment"; const COLL_FFR_SOURCE = "FlatRateFulfillmentRestrictions"; const COLL_FFR_DEST = "FulfillmentRestrictions"; +const FF_TYPE = "shipping"; +const FF_METHOD = "flatRate"; +const COLL_DEST = "Fulfillment"; + +const newGlobalPermissions = [ + "reaction:legacy:fulfillmentTypes/update:settings", + "reaction:legacy:fulfillmentRestrictions/create", + "reaction:legacy:fulfillmentRestrictions/delete", + "reaction:legacy:fulfillmentRestrictions/read", + "reaction:legacy:fulfillmentRestrictions/update", + "reaction:legacy:fulfillmentTypes/create", + "reaction:legacy:fulfillmentTypes/delete", + "reaction:legacy:fulfillmentTypes/read", + "reaction:legacy:fulfillmentTypes/update", + "reaction:legacy:fulfillmentMethods/create", + "reaction:legacy:fulfillmentMethods/delete", + "reaction:legacy:fulfillmentMethods/read", + "reaction:legacy:fulfillmentMethods/update" +]; /** * @summary migrates the database down one version @@ -14,6 +33,23 @@ const COLL_FFR_DEST = "FulfillmentRestrictions"; async function down({ db, progress }) { progress(0); + const allGroups = await db.collection("Groups").find({}).toArray(); + const affectedGlobalGroups = []; + allGroups.forEach((group) => { + const perms = group.permissions; + if (perms && Array.isArray(perms) && perms.length) { + const found = newGlobalPermissions.some((elem) => perms.includes(elem)); + if (found) affectedGlobalGroups.push(group.slug); + } + }); + + await db.collection("Groups").updateMany({ + slug: { $in: affectedGlobalGroups } + }, { + $pullAll: { permissions: newGlobalPermissions } + }); + progress(10); + await db.collection(COLL_FF_DEST).drop(); progress(50); @@ -34,10 +70,104 @@ async function up({ db, progress }) { const shippingCopyResp = await db.collection(COLL_FF_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FF_DEST }]).next(); if (shippingCopyResp) throw new Error("Error in copying Shipping collection"); // above command returns null if successful - progress(50); + progress(20); const flatRateRestCopyResp = await db.collection(COLL_FFR_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FFR_DEST }]).next(); if (flatRateRestCopyResp) throw new Error("Error in copying FlatRateFulfillmentRestrictions collection"); // above command returns null if successful + progress(40); + + const operations = []; + + const ffTypeUpdate = { + updateMany: { + filter: { fulfillmentType: { $exists: false } }, + update: { + $set: { + fulfillmentType: FF_TYPE + } + } + } + }; + + const ffMethodUpdate = { + updateMany: { + filter: { methods: { $exists: true } }, + update: { + $set: { + "methods.$[eachMethod].fulfillmentMethod": FF_METHOD + } + }, + arrayFilters: [ + { + "eachMethod.fulfillmentMethod": { $exists: false } + } + ] + } + }; + + operations.push(ffTypeUpdate); + operations.push(ffMethodUpdate); + + const bulkWriteResp = await db.collection(COLL_DEST).bulkWrite(operations); + if (bulkWriteResp.writeErrors && bulkWriteResp.writeErrors.length) throw new Error("Error while updating Fulfillment collection"); + progress(50); + + const oldPerms = [ + "reaction:legacy:shippingMethods/create", + "reaction:legacy:shippingMethods/delete", + "reaction:legacy:shippingMethods/read", + "reaction:legacy:shippingMethods/update", + "reaction:legacy:shippingRestrictions/create", + "reaction:legacy:shippingRestrictions/delete", + "reaction:legacy:shippingRestrictions/read", + "reaction:legacy:shippingRestrictions/update" + ]; + + const mapperSetFFMethodsRestricts = { + "reaction:legacy:shippingMethods/create": "reaction:legacy:fulfillmentMethods/create", + "reaction:legacy:shippingMethods/delete": "reaction:legacy:fulfillmentMethods/delete", + "reaction:legacy:shippingMethods/read": "reaction:legacy:fulfillmentMethods/read", + "reaction:legacy:shippingMethods/update": "reaction:legacy:fulfillmentMethods/update", + "reaction:legacy:shippingRestrictions/create": "reaction:legacy:fulfillmentRestrictions/create", + "reaction:legacy:shippingRestrictions/delete": "reaction:legacy:fulfillmentRestrictions/delete", + "reaction:legacy:shippingRestrictions/read": "reaction:legacy:fulfillmentRestrictions/read", + "reaction:legacy:shippingRestrictions/update": "reaction:legacy:fulfillmentRestrictions/update" + }; + const mapperSetFFTypes = { + "reaction:legacy:shippingMethods/create": "reaction:legacy:fulfillmentTypes/create", + "reaction:legacy:shippingMethods/delete": "reaction:legacy:fulfillmentTypes/delete", + "reaction:legacy:shippingMethods/read": "reaction:legacy:fulfillmentTypes/read", + "reaction:legacy:shippingMethods/update": "reaction:legacy:fulfillmentTypes/update" + }; + const allGroups = await db.collection("Groups").find({}).toArray(); + + for (let index = 0; index < allGroups.length; index += 1) { + const currentGroup = allGroups[index]; + const currentPerms = currentGroup.permissions; + const permsToAdd = []; + + if (currentPerms && Array.isArray(currentPerms) && currentPerms.length) { + oldPerms.forEach((oldPerm) => { + if (currentPerms.includes(oldPerm)) { + permsToAdd.push(mapperSetFFMethodsRestricts[oldPerm]); + if (mapperSetFFTypes[oldPerm]) { + permsToAdd.push(mapperSetFFTypes[oldPerm]); + } + } + }); + } + if (permsToAdd.length) { + permsToAdd.push("reaction:legacy:fulfillmentTypes/update:settings"); // add this setting to groups deailing with ff-types + // eslint-disable-next-line no-await-in-loop + await db.collection("Groups").updateOne( + { _id: currentGroup._id }, + { + $addToSet: { permissions: { $each: permsToAdd } } + } + ); + } + } + progress(100); } diff --git a/packages/api-plugin-fulfillment/migrations/3.js b/packages/api-plugin-fulfillment/migrations/3.js deleted file mode 100644 index e1b1506034e..00000000000 --- a/packages/api-plugin-fulfillment/migrations/3.js +++ /dev/null @@ -1,56 +0,0 @@ -const FF_TYPE = "shipping"; -const FF_METHOD = "flatRate"; -const COLL_DEST = "Fulfillment"; - -/** - * @summary Performs migration up from previous data version - * @param {Object} context Migration context - * @param {Object} context.db MongoDB `Db` instance - * @param {Function} context.progress A function to report progress, takes percent - * number as argument. - * @return {undefined} - */ -async function up({ db, progress }) { - progress(0); - - const operations = []; - - const ffTypeUpdate = { - updateMany: { - filter: { fulfillmentType: { $exists: false } }, - update: { - $set: { - fulfillmentType: FF_TYPE - } - } - } - }; - - const ffMethodUpdate = { - updateMany: { - filter: { methods: { $exists: true } }, - update: { - $set: { - "methods.$[eachMethod].fulfillmentMethod": FF_METHOD - } - }, - arrayFilters: [ - { - "eachMethod.fulfillmentMethod": { $exists: false } - } - ] - } - }; - - operations.push(ffTypeUpdate); - operations.push(ffMethodUpdate); - - const bulkWriteResp = await db.collection(COLL_DEST).bulkWrite(operations); - if (bulkWriteResp.writeErrors && bulkWriteResp.writeErrors.length) throw new Error("Error while updating Fulfillment collection"); - progress(100); -} - -export default { - down: "unnecessary", // The newly created collections would get dropped in stage2 - up -}; diff --git a/packages/api-plugin-fulfillment/migrations/4.js b/packages/api-plugin-fulfillment/migrations/4.js deleted file mode 100644 index af2dda9bbdf..00000000000 --- a/packages/api-plugin-fulfillment/migrations/4.js +++ /dev/null @@ -1,64 +0,0 @@ -const affectedGlobalGroups = [ - "owner", - "shop manager" -]; - -const newGlobalPermissions = [ - "reaction:legacy:fulfillmentRestrictions/create", - "reaction:legacy:fulfillmentRestrictions/delete", - "reaction:legacy:fulfillmentRestrictions/read", - "reaction:legacy:fulfillmentRestrictions/update", - "reaction:legacy:fulfillmentTypes/create", - "reaction:legacy:fulfillmentTypes/delete", - "reaction:legacy:fulfillmentTypes/read", - "reaction:legacy:fulfillmentTypes/update", - "reaction:legacy:fulfillmentMethods/create", - "reaction:legacy:fulfillmentMethods/delete", - "reaction:legacy:fulfillmentMethods/read", - "reaction:legacy:fulfillmentMethods/update" -]; - -/** - * @summary migrates the database down one version - * @param {Object} context Migration context - * @param {Object} context.db MongoDB `Db` instance - * @param {Function} context.progress A function to report progress, takes percent - * number as argument. - * @return {undefined} - */ -async function down({ db, progress }) { - progress(0); - - await db.collection("Groups").updateMany({ - slug: { $in: affectedGlobalGroups } - }, { - $pullAll: { permissions: newGlobalPermissions } - }); - - progress(100); -} - -/** - * @summary Performs migration up from previous data version - * @param {Object} context Migration context - * @param {Object} context.db MongoDB `Db` instance - * @param {Function} context.progress A function to report progress, takes percent - * number as argument. - * @return {undefined} - */ -async function up({ db, progress }) { - progress(0); - - await db.collection("Groups").updateMany({ - slug: { $in: affectedGlobalGroups } - }, { - $addToSet: { permissions: { $each: newGlobalPermissions } } - }); - - progress(100); -} - -export default { - down, - up -}; diff --git a/packages/api-plugin-fulfillment/migrations/index.js b/packages/api-plugin-fulfillment/migrations/index.js index 783e2bf9115..d6ef9ab5586 100644 --- a/packages/api-plugin-fulfillment/migrations/index.js +++ b/packages/api-plugin-fulfillment/migrations/index.js @@ -1,16 +1,12 @@ import { migrationsNamespace } from "./migrationsNamespace.js"; import migration2 from "./2.js"; -import migration3 from "./3.js"; -import migration4 from "./4.js"; export default { tracks: [ { namespace: migrationsNamespace, migrations: { - 2: migration2, - 3: migration3, - 4: migration4 + 2: migration2 } } ] From 3404407f5f414e87f51eb08fadbbf34788ed8252 Mon Sep 17 00:00:00 2001 From: Sujith Date: Mon, 7 Nov 2022 19:15:43 +0530 Subject: [PATCH 7/8] fix: pnpm-lock without snyk Signed-off-by: Sujith --- pnpm-lock.yaml | 42 ++---------------------------------------- 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a54914d1c58..2ea48d291c5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -678,6 +678,7 @@ importers: '@reactioncommerce/api-plugin-carts': ^1.0.0 '@reactioncommerce/api-utils': ^1.16.9 '@reactioncommerce/data-factory': ~1.0.1 + '@reactioncommerce/db-version-check': ^1.0.0 '@reactioncommerce/logger': ^1.1.3 '@reactioncommerce/random': ^1.0.2 '@reactioncommerce/reaction-error': ^1.0.1 @@ -685,6 +686,7 @@ importers: simpl-schema: ^1.12.2 dependencies: '@reactioncommerce/api-utils': link:../api-utils + '@reactioncommerce/db-version-check': link:../db-version-check '@reactioncommerce/logger': link:../logger '@reactioncommerce/random': link:../random '@reactioncommerce/reaction-error': link:../reaction-error @@ -694,46 +696,6 @@ importers: '@reactioncommerce/api-plugin-carts': link:../api-plugin-carts '@reactioncommerce/data-factory': 1.0.1 - packages/api-plugin-fulfillment-method-shipping-flat-rate: - specifiers: - '@reactioncommerce/api-utils': ^1.16.9 - '@reactioncommerce/logger': ^1.1.3 - '@reactioncommerce/random': ~1.0.2 - '@reactioncommerce/reaction-error': ^1.0.1 - simpl-schema: ^1.12.2 - dependencies: - '@reactioncommerce/api-utils': link:../api-utils - '@reactioncommerce/logger': link:../logger - '@reactioncommerce/random': link:../random - '@reactioncommerce/reaction-error': link:../reaction-error - simpl-schema: 1.12.3 - - packages/api-plugin-fulfillment-method-shipping-ups: - specifiers: - '@reactioncommerce/api-utils': ^1.16.9 - '@reactioncommerce/reaction-error': ^1.0.1 - simpl-schema: ^1.12.2 - dependencies: - '@reactioncommerce/api-utils': link:../api-utils - '@reactioncommerce/reaction-error': link:../reaction-error - simpl-schema: 1.12.3 - - packages/api-plugin-fulfillment-type-shipping: - specifiers: - '@reactioncommerce/api-utils': ^1.16.9 - '@reactioncommerce/logger': ^1.1.3 - '@reactioncommerce/random': ~1.0.2 - '@reactioncommerce/reaction-error': ^1.0.1 - lodash: ^4.17.21 - simpl-schema: ^1.12.2 - dependencies: - '@reactioncommerce/api-utils': link:../api-utils - '@reactioncommerce/logger': link:../logger - '@reactioncommerce/random': link:../random - '@reactioncommerce/reaction-error': link:../reaction-error - lodash: 4.17.21 - simpl-schema: 1.12.3 - packages/api-plugin-i18n: specifiers: '@reactioncommerce/api-utils': ^1.16.5 From 13bab48f2fbfcefe9dbbd6ba4e25dac8f9563d71 Mon Sep 17 00:00:00 2001 From: Sujith Date: Thu, 18 May 2023 18:00:31 +0530 Subject: [PATCH 8/8] fix: linter error Signed-off-by: Sujith --- packages/api-plugin-fulfillment/src/preStartup.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/api-plugin-fulfillment/src/preStartup.js b/packages/api-plugin-fulfillment/src/preStartup.js index d76a69f3415..6cd3b1dcac8 100644 --- a/packages/api-plugin-fulfillment/src/preStartup.js +++ b/packages/api-plugin-fulfillment/src/preStartup.js @@ -1,10 +1,7 @@ -import Logger from "@reactioncommerce/logger"; -import ReactionError from "@reactioncommerce/reaction-error"; import doesDatabaseVersionMatch from "@reactioncommerce/db-version-check"; import { migrationsNamespace } from "../migrations/migrationsNamespace.js"; import { extendFulfillmentSchemas } from "./simpleSchemas.js"; -const logCtx = { name: "fulfillment", file: "preStartup" }; const expectedVersion = 4; /**