agrs-sequelize-sdk 1.0.89 → 1.0.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/models/AdSet.js CHANGED
@@ -135,5 +135,30 @@ module.exports = (sequelize, DataTypes) => {
135
135
  }
136
136
  });
137
137
 
138
- return AdSet;
139
- };
138
+ AdSet.addHook("beforeUpdate", async (adSet, options) => {
139
+ // Check if AdSetID is about to change
140
+ if (adSet.changed("AdSetID")) {
141
+ const oldAdSetID = adSet.previous("AdSetID");
142
+ const newAdSetID = adSet.AdSetID;
143
+
144
+ const transaction = options.transaction || await sequelize.transaction(); // Use or create transaction
145
+
146
+ try {
147
+ // Update associated Ads to the new AdSetID
148
+ await sequelize.models.Ad.update(
149
+ { AdSetID: newAdSetID }, // Set new AdSetID
150
+ { where: { AdSetID: oldAdSetID }, transaction }
151
+ );
152
+
153
+ if (!options.transaction) {
154
+ await transaction.commit();
155
+ }
156
+ } catch (error) {
157
+ if (!options.transaction) {
158
+ await transaction.rollback();
159
+ }
160
+ throw error; // Propagate the error
161
+ }
162
+ }
163
+ });
164
+
@@ -127,6 +127,34 @@ module.exports = (sequelize, DataTypes) => {
127
127
  }
128
128
  });
129
129
 
130
-
131
- return Campaign;
132
- };
130
+
131
+ Campaign.addHook("beforeUpdate", async (campaign, options) => {
132
+ // Check if CampaignID is about to change
133
+ if (campaign.changed("CampaignID")) {
134
+ const oldCampaignID = campaign.previous("CampaignID");
135
+ const newCampaignID = campaign.CampaignID;
136
+
137
+ // Use existing transaction or create a new one
138
+ const transaction = options.transaction || await sequelize.transaction();
139
+
140
+ try {
141
+ // Update all associated AdSets to the new CampaignID
142
+ await sequelize.models.AdSet.update(
143
+ { CampaignID: newCampaignID }, // Set new CampaignID
144
+ { where: { CampaignID: oldCampaignID }, transaction }
145
+ );
146
+
147
+ // Commit transaction if this is a new transaction
148
+ if (!options.transaction) {
149
+ await transaction.commit();
150
+ }
151
+ } catch (error) {
152
+ // Rollback on failure
153
+ if (!options.transaction) {
154
+ await transaction.rollback();
155
+ }
156
+ throw error; // Propagate the error
157
+ }
158
+ }
159
+ });
160
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.0.89",
3
+ "version": "1.0.91",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",