Skip to content

Commit 6180641

Browse files
committed
Added a cron job to ShelterSupply module to run every 30 minutes to change priority of expired supplies (urgent older than 4 hours)
1 parent 2895265 commit 6180641

File tree

8 files changed

+128
-1
lines changed

8 files changed

+128
-1
lines changed

package-lock.json

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@nestjs/passport": "^10.0.3",
2828
"@nestjs/platform-express": "^10.0.0",
2929
"@nestjs/platform-fastify": "^10.3.8",
30+
"@nestjs/schedule": "^4.0.2",
3031
"@nestjs/swagger": "^7.3.1",
3132
"@prisma/client": "^5.13.0",
3233
"bcrypt": "^5.1.1",

src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
22
import { APP_INTERCEPTOR } from '@nestjs/core';
3+
import { ScheduleModule } from '@nestjs/schedule';
34

45
import { PrismaModule } from './prisma/prisma.module';
56
import { ShelterModule } from './shelter/shelter.module';
@@ -15,6 +16,7 @@ import { ShelterSupplyModule } from './shelter-supply/shelter-supply.module';
1516
@Module({
1617
imports: [
1718
PrismaModule,
19+
ScheduleModule.forRoot(),
1820
UsersModule,
1921
SessionsModule,
2022
ShelterModule,

src/shelter-supply/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const priorityExpiryInterval = 4; // In hours
2+
3+
export { priorityExpiryInterval };
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Injectable, Logger } from '@nestjs/common';
2+
import { Cron, CronExpression } from '@nestjs/schedule';
3+
import { PrismaService } from '../prisma/prisma.service';
4+
import { addHours } from '@/utils';
5+
import { priorityExpiryInterval } from './constants';
6+
import { SupplyPriority } from '../supply/types';
7+
import { ShelterSupplyService } from './shelter-supply.service';
8+
9+
@Injectable()
10+
export class ShelterSupplyJob {
11+
constructor(
12+
private readonly prismaService: PrismaService,
13+
private readonly shelterSupplyService: ShelterSupplyService,
14+
) {}
15+
16+
private readonly logger = new Logger(ShelterSupplyJob.name);
17+
18+
@Cron(CronExpression.EVERY_30_MINUTES)
19+
async handleCron() {
20+
this.logger.log(`${ShelterSupplyJob.name} running`);
21+
22+
const expiryDate = addHours(
23+
new Date(Date.now()),
24+
-priorityExpiryInterval,
25+
).toString();
26+
27+
const outatedSupplies = await this.prismaService.shelterSupply.findMany({
28+
where: {
29+
AND: [
30+
{ priority: SupplyPriority.Urgent },
31+
{
32+
OR: [
33+
{
34+
createdAt: {
35+
lte: expiryDate,
36+
},
37+
},
38+
{
39+
updatedAt: {
40+
lte: expiryDate,
41+
},
42+
},
43+
],
44+
},
45+
],
46+
},
47+
select: {
48+
supplyId: true,
49+
shelterId: true,
50+
},
51+
});
52+
53+
outatedSupplies.forEach(async (s) => {
54+
const { shelterId, supplyId } = s;
55+
await this.shelterSupplyService.update({
56+
data: {
57+
priority: SupplyPriority.Needing,
58+
},
59+
where: { shelterId: shelterId, supplyId: supplyId },
60+
});
61+
});
62+
}
63+
}

src/shelter-supply/shelter-supply.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { Module } from '@nestjs/common';
33
import { ShelterSupplyService } from './shelter-supply.service';
44
import { ShelterSupplyController } from './shelter-supply.controller';
55
import { PrismaModule } from '../prisma/prisma.module';
6+
import { ShelterSupplyJob } from './shelter-supply.job';
67

78
@Module({
89
imports: [PrismaModule],
9-
providers: [ShelterSupplyService],
10+
providers: [ShelterSupplyService, ShelterSupplyJob],
1011
controllers: [ShelterSupplyController],
1112
})
1213
export class ShelterSupplyModule {}

src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getSearchWhere,
77
parseStringToObject,
88
capitalize,
9+
addHours,
910
} from './utils';
1011

1112
export {
@@ -16,4 +17,5 @@ export {
1617
deepMerge,
1718
getSearchWhere,
1819
parseStringToObject,
20+
addHours,
1921
};

src/utils/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ function getSearchWhere(search: string, or?: boolean) {
140140
} else return where;
141141
}
142142

143+
function addHours(date: Date, hours: number) {
144+
const result = new Date();
145+
result.setTime(date.getTime() + hours * 60 * 60 * 1000);
146+
return result;
147+
}
148+
143149
export {
144150
ServerResponse,
145151
removeNotNumbers,
@@ -148,4 +154,5 @@ export {
148154
parseStringToObject,
149155
getSearchWhere,
150156
capitalize,
157+
addHours,
151158
};

0 commit comments

Comments
 (0)