agrs-sequelize-sdk 1.1.19 → 1.1.21
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/KeywordPerformance.js +18 -36
- package/package.json +1 -1
|
@@ -64,44 +64,26 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
64
64
|
hooks: {
|
|
65
65
|
beforeCreate: async (record, options) => {
|
|
66
66
|
try {
|
|
67
|
-
// Find
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
SELECT MIN("Date") as first_date
|
|
71
|
-
FROM "KeywordPerformance"
|
|
72
|
-
WHERE LOWER("Keyword") = LOWER(:keyword)
|
|
73
|
-
`,
|
|
74
|
-
{
|
|
75
|
-
replacements: { keyword: record.Keyword },
|
|
76
|
-
type: sequelize.QueryTypes.SELECT,
|
|
77
|
-
transaction: options.transaction,
|
|
78
|
-
}
|
|
67
|
+
// Find existing record for this keyword
|
|
68
|
+
console.log(
|
|
69
|
+
"I got to beforeCreate hook in the KeywordPerformance model"
|
|
79
70
|
);
|
|
71
|
+
const existingKeyword =
|
|
72
|
+
await sequelize.models.KeywordPerformance.findOne({
|
|
73
|
+
where: {
|
|
74
|
+
Keyword: sequelize.where(
|
|
75
|
+
sequelize.fn("LOWER", sequelize.col("Keyword")),
|
|
76
|
+
sequelize.fn("LOWER", record.Keyword)
|
|
77
|
+
),
|
|
78
|
+
},
|
|
79
|
+
attributes: ["FirstAppearanceDate"],
|
|
80
|
+
transaction: options.transaction,
|
|
81
|
+
});
|
|
80
82
|
|
|
81
|
-
// If
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
? Math.min(new Date(existingFirstDate), new Date(record.Date))
|
|
86
|
-
: record.Date;
|
|
87
|
-
|
|
88
|
-
// If we found an earlier date, update all existing records for this keyword
|
|
89
|
-
if (existingFirstDate && record.Date < existingFirstDate) {
|
|
90
|
-
await sequelize.query(
|
|
91
|
-
`
|
|
92
|
-
UPDATE "KeywordPerformance"
|
|
93
|
-
SET "FirstAppearanceDate" = :newDate
|
|
94
|
-
WHERE LOWER("Keyword") = LOWER(:keyword)
|
|
95
|
-
`,
|
|
96
|
-
{
|
|
97
|
-
replacements: {
|
|
98
|
-
newDate: record.Date,
|
|
99
|
-
keyword: record.Keyword,
|
|
100
|
-
},
|
|
101
|
-
transaction: options.transaction,
|
|
102
|
-
}
|
|
103
|
-
);
|
|
104
|
-
}
|
|
83
|
+
// If keyword exists, use its FirstAppearanceDate
|
|
84
|
+
// If not, use the current record's Date
|
|
85
|
+
record.FirstAppearanceDate =
|
|
86
|
+
existingKeyword?.FirstAppearanceDate || record.Date;
|
|
105
87
|
} catch (error) {
|
|
106
88
|
console.error("Error in FirstAppearanceDate hook:", error);
|
|
107
89
|
throw error;
|