agrs-sequelize-sdk 1.0.86 → 1.0.88

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/Ad.js CHANGED
@@ -168,27 +168,16 @@ module.exports = (sequelize, DataTypes) => {
168
168
  return null;
169
169
  };
170
170
 
171
- Ad.addHook("beforeCreate", async (ad, options) => {
172
- if (ad.changed("publish")) {
173
- if (ad.previous("publish") === true && ad.publish === false) {
174
- // Save current state to AdHistory
175
- await sequelize.models.AdHistory.create({
176
- AdID: ad.AdID,
177
- DataSnapshot: ad._previousDataValues, // Save the full Ad data as JSON
178
- });
179
- } else if (ad.previous("publish") === false && ad.publish === true) {
180
- // Delete the latest history entry
181
- const latestHistory = await sequelize.models.AdHistory.findOne({
182
- where: { AdID: ad.AdID },
183
- order: [["timestamp", "DESC"]], // Get the latest history entry
184
- });
185
-
186
- if (latestHistory) {
187
- await latestHistory.destroy();
188
- }
189
- }
171
+ Ad.addHook("afterCreate", async (ad, options) => {
172
+ if (ad.publish === false) {
173
+ // If publish is false on creation, save a snapshot to AdHistory
174
+ await sequelize.models.AdHistory.create({
175
+ AdID: ad.AdID,
176
+ DataSnapshot: ad.toJSON(), // Save the full Ad data as JSON
177
+ });
190
178
  }
191
179
  });
180
+
192
181
  Ad.addHook("beforeUpdate", async (ad, options) => {
193
182
  if (ad.changed("publish")) {
194
183
  if (ad.previous("publish") === true && ad.publish === false) {
package/models/AdSet.js CHANGED
@@ -124,30 +124,16 @@ module.exports = (sequelize, DataTypes) => {
124
124
  }
125
125
  }
126
126
  });
127
-
128
- AdSet.addHook("beforeCreate", async (adSet, options) => {
129
- if (adSet.changed("publish")) {
130
- if (adSet.previous("publish") === true && adSet.publish === false) {
131
- // Save current state to AdSetHistory
132
- await sequelize.models.AdSetHistory.create({
133
- AdSetID: adSet.AdSetID,
134
- DataSnapshot: adSet._previousDataValues, // Save the full AdSet data as JSON
135
- });
136
- } else if (
137
- adSet.previous("publish") === false &&
138
- adSet.publish === true
139
- ) {
140
- // Delete the latest history entry
141
- const latestHistory = await sequelize.models.AdSetHistory.findOne({
142
- where: { AdSetID: adSet.AdSetID },
143
- order: [["timestamp", "DESC"]], // Get the latest history entry
144
- });
145
127
 
146
- if (latestHistory) {
147
- await latestHistory.destroy();
148
- }
149
- }
128
+ AdSet.addHook("afterCreate", async (adSet, options) => {
129
+ if (adSet.publish === false) {
130
+ // If publish is false on creation, save a snapshot to AdSetHistory
131
+ await sequelize.models.AdSetHistory.create({
132
+ AdSetID: adSet.AdSetID,
133
+ DataSnapshot: adSet.toJSON(), // Save the full AdSet data as JSON
134
+ });
150
135
  }
151
136
  });
137
+
152
138
  return AdSet;
153
139
  };
@@ -117,30 +117,16 @@ module.exports = (sequelize, DataTypes) => {
117
117
  }
118
118
  });
119
119
 
120
- Campaign.addHook("beforeCreate", async (campaign, options) => {
121
- if (campaign.changed("publish")) {
122
- if (campaign.previous("publish") === true && campaign.publish === false) {
123
- // Save current state to CampaignHistory
124
- await sequelize.models.CampaignHistory.create({
125
- CampaignID: campaign.CampaignID,
126
- DataSnapshot: campaign._previousDataValues, // Store current state as JSON
127
- });
128
- } else if (
129
- campaign.previous("publish") === false &&
130
- campaign.publish === true
131
- ) {
132
- // Delete the latest history entry
133
- const latestHistory = await sequelize.models.CampaignHistory.findOne({
134
- where: { CampaignID: campaign.CampaignID },
135
- order: [["timestamp", "DESC"]], // Get the latest history entry
136
- });
137
-
138
- if (latestHistory) {
139
- await latestHistory.destroy(); // Delete the latest history entry
140
- }
141
- }
120
+ Campaign.addHook("afterCreate", async (campaign, options) => {
121
+ if (campaign.publish === false) {
122
+ // If publish is false on creation, save a snapshot to CampaignHistory
123
+ await sequelize.models.CampaignHistory.create({
124
+ CampaignID: campaign.CampaignID,
125
+ DataSnapshot: campaign.toJSON(), // Save current state
126
+ });
142
127
  }
143
128
  });
129
+
144
130
 
145
131
  return Campaign;
146
132
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.0.86",
3
+ "version": "1.0.88",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",