agrs-sequelize-sdk 1.0.91 → 1.0.92

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.
Files changed (2) hide show
  1. package/models/AdSet.js +51 -54
  2. package/package.json +1 -1
package/models/AdSet.js CHANGED
@@ -31,18 +31,18 @@ module.exports = (sequelize, DataTypes) => {
31
31
  allowNull: true,
32
32
  },
33
33
  promotedObject: {
34
- type: DataTypes.JSONB, // Storing promoted object as a JSON object
34
+ type: DataTypes.JSONB,
35
35
  allowNull: true,
36
36
  },
37
37
  BillingEvent: {
38
38
  type: DataTypes.STRING,
39
39
  allowNull: false,
40
- defaultValue: "impressions", // Default value for BillingEvent
40
+ defaultValue: "impressions",
41
41
  },
42
42
  OptimizationGoal: {
43
43
  type: DataTypes.STRING,
44
44
  allowNull: false,
45
- defaultValue: "offsite_conversions", // Default value for OptimizationGoal
45
+ defaultValue: "offsite_conversions",
46
46
  },
47
47
  Draft: {
48
48
  type: DataTypes.BOOLEAN,
@@ -70,7 +70,6 @@ module.exports = (sequelize, DataTypes) => {
70
70
  type: DataTypes.STRING,
71
71
  allowNull: true,
72
72
  },
73
- // add issues_info field
74
73
  issuesInfo: {
75
74
  type: DataTypes.JSONB,
76
75
  allowNull: true,
@@ -79,7 +78,6 @@ module.exports = (sequelize, DataTypes) => {
79
78
  type: DataTypes.STRING,
80
79
  allowNull: true,
81
80
  },
82
- // bid amount
83
81
  bid_amount: {
84
82
  type: DataTypes.INTEGER,
85
83
  allowNull: true,
@@ -89,7 +87,7 @@ module.exports = (sequelize, DataTypes) => {
89
87
  tableName: "AdSet",
90
88
  indexes: [
91
89
  {
92
- fields: ["CampaignID"], // Index on CampaignID for faster join
90
+ fields: ["CampaignID"],
93
91
  },
94
92
  ],
95
93
  }
@@ -100,65 +98,64 @@ module.exports = (sequelize, DataTypes) => {
100
98
  AdSet.hasMany(models.Ad, { foreignKey: "AdSetID" });
101
99
  };
102
100
 
101
+ // Consolidated beforeUpdate hook
103
102
  AdSet.addHook("beforeUpdate", async (adSet, options) => {
104
- if (adSet.changed("publish")) {
105
- if (adSet.previous("publish") === true && adSet.publish === false) {
106
- // Save current state to AdSetHistory
107
- await sequelize.models.AdSetHistory.create({
108
- AdSetID: adSet.AdSetID,
109
- DataSnapshot: adSet._previousDataValues, // Save the full AdSet data as JSON
110
- });
111
- } else if (
112
- adSet.previous("publish") === false &&
113
- adSet.publish === true
114
- ) {
115
- // Delete the latest history entry
116
- const latestHistory = await sequelize.models.AdSetHistory.findOne({
117
- where: { AdSetID: adSet.AdSetID },
118
- order: [["timestamp", "DESC"]], // Get the latest history entry
119
- });
103
+ const transaction = options.transaction || await sequelize.transaction();
104
+
105
+ try {
106
+ // Check if AdSetID is about to change
107
+ if (adSet.changed("AdSetID")) {
108
+ const oldAdSetID = adSet.previous("AdSetID");
109
+ const newAdSetID = adSet.AdSetID;
110
+
111
+ // Update associated Ads to the new AdSetID
112
+ await sequelize.models.Ad.update(
113
+ { AdSetID: newAdSetID },
114
+ { where: { AdSetID: oldAdSetID }, transaction }
115
+ );
116
+ }
117
+
118
+ // Handle 'publish' changes
119
+ if (adSet.changed("publish")) {
120
+ if (adSet.previous("publish") === true && adSet.publish === false) {
121
+ await sequelize.models.AdSetHistory.create({
122
+ AdSetID: adSet.AdSetID,
123
+ DataSnapshot: adSet._previousDataValues,
124
+ });
125
+ } else if (
126
+ adSet.previous("publish") === false &&
127
+ adSet.publish === true
128
+ ) {
129
+ const latestHistory = await sequelize.models.AdSetHistory.findOne({
130
+ where: { AdSetID: adSet.AdSetID },
131
+ order: [["timestamp", "DESC"]],
132
+ });
120
133
 
121
- if (latestHistory) {
122
- await latestHistory.destroy();
134
+ if (latestHistory) {
135
+ await latestHistory.destroy();
136
+ }
123
137
  }
124
138
  }
139
+
140
+ if (!options.transaction) {
141
+ await transaction.commit();
142
+ }
143
+ } catch (error) {
144
+ if (!options.transaction) {
145
+ await transaction.rollback();
146
+ }
147
+ throw error;
125
148
  }
126
149
  });
127
150
 
128
151
  AdSet.addHook("afterCreate", async (adSet, options) => {
129
152
  if (adSet.publish === false) {
130
- // If publish is false on creation, save a snapshot to AdSetHistory
131
153
  await sequelize.models.AdSetHistory.create({
132
154
  AdSetID: adSet.AdSetID,
133
- DataSnapshot: adSet.toJSON(), // Save the full AdSet data as JSON
155
+ DataSnapshot: adSet.toJSON(),
134
156
  });
135
157
  }
136
158
  });
137
-
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
-
159
+
160
+ return AdSet;
161
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.0.91",
3
+ "version": "1.0.92",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",