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