Skip to content

Commit 941e873

Browse files
Merge pull request #1954 from OneCommunityGlobal/venkataramanan_filter_blue_square_one_year_old
Venkataramanan 🔥 delete blue squares one year old
2 parents 0f0d6d8 + 83663ec commit 941e873

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

src/controllers/userProfileController.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,9 @@ const userProfileController = function (UserProfile, Project) {
10421042
// res.status(200).send(getData);
10431043
// return;
10441044
// }
1045+
const ONE_YEAR_AGO = new Date();
1046+
ONE_YEAR_AGO.setFullYear(ONE_YEAR_AGO.getFullYear() - 1);
1047+
10451048
UserProfile.findById(userid, '-password -refreshTokens -lastModifiedDate -__v')
10461049
.populate([
10471050
{
@@ -1072,6 +1075,7 @@ const userProfileController = function (UserProfile, Project) {
10721075
},
10731076
{
10741077
path: 'infringements', // Populate infringements field
1078+
match: { date: { $gte: ONE_YEAR_AGO } },
10751079
select: 'date description',
10761080
options: {
10771081
sort: {

src/helpers/userHelper.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,30 +1138,29 @@ const userHelper = function () {
11381138
};
11391139

11401140
const deleteBlueSquareAfterYear = async () => {
1141-
const currentFormattedDate = moment().tz('America/Los_Angeles').format();
1141+
const nowLA = moment().tz('America/Los_Angeles');
11421142

1143-
logger.logInfo(
1144-
`Job for deleting blue squares older than 1 year starting at ${currentFormattedDate}`,
1145-
);
1143+
logger.logInfo(`Job for deleting blue squares older than 1 year starting at ${nowLA.format()}`);
11461144

1147-
const cutOffDate = moment().subtract(1, 'year').format('YYYY-MM-DD');
1145+
const cutOffDate = nowLA.clone().subtract(1, 'year').toDate();
11481146

11491147
try {
11501148
const results = await userProfile.updateMany(
11511149
{},
11521150
{
11531151
$pull: {
11541152
infringements: {
1155-
date: {
1156-
$lte: cutOffDate,
1157-
},
1153+
date: { $lte: cutOffDate },
11581154
},
11591155
},
11601156
},
11611157
);
11621158

1163-
logger.logInfo(`Job deleting blue squares older than 1 year finished
1164-
at ${moment().tz('America/Los_Angeles').format()} \nReulst: ${JSON.stringify(results)}`);
1159+
logger.logInfo(
1160+
`Job deleting blue squares older than 1 year finished at ${moment()
1161+
.tz('America/Los_Angeles')
1162+
.format()} \nResult: ${JSON.stringify(results)}`,
1163+
);
11651164
} catch (err) {
11661165
logger.logException(err);
11671166
}

0 commit comments

Comments
 (0)