Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ DATABASE_URL="postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_
SECRET_KEY=batata

HOST=::0.0.0.0
PORT=4000
PORT=4000

# In hours
SHELTER_SUPPLY_PRIORITY_EXPIRY_INTERVAL=4

# Every 30 minutes
SHELTER_SUPPLY_PRIORITY_JOB_INTERVAL="0 */30 * * * *"
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { ScheduleModule } from '@nestjs/schedule';

import { PrismaModule } from './prisma/prisma.module';
import { ShelterModule } from './shelter/shelter.module';
Expand All @@ -15,6 +16,7 @@ import { ShelterSupplyModule } from './shelter-supply/shelter-supply.module';
@Module({
imports: [
PrismaModule,
ScheduleModule.forRoot(),
UsersModule,
SessionsModule,
ShelterModule,
Expand Down
8 changes: 8 additions & 0 deletions src/shelter-supply/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const jobInterval: string =
process.env.SHELTER_SUPPLY_PRIORITY_JOB_INTERVAL ?? '0 */30 * * * *';

const priorityExpiryInterval: number = Number.parseInt(
process.env.SHELTER_SUPPLY_PRIORITY_EXPIRY_INTERVAL ?? '4',
);

export { jobInterval, priorityExpiryInterval };
35 changes: 35 additions & 0 deletions src/shelter-supply/shelter-supply-expiration.job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { PrismaService } from '../prisma/prisma.service';
import { addHours } from '@/utils';
import { priorityExpiryInterval, jobInterval } from './constants';
import { SupplyPriority } from '../supply/types';
import { ShelterSupplyService } from './shelter-supply.service';

@Injectable()
export class ShelterSupplyExpirationJob {
constructor(
private readonly prismaService: PrismaService,
private readonly shelterSupplyService: ShelterSupplyService,
) {}

private readonly logger = new Logger(ShelterSupplyExpirationJob.name);

@Cron(jobInterval)
async handleCron() {
this.logger.log(`${ShelterSupplyExpirationJob.name} running`);

const expiryDate = addHours(new Date(Date.now()), -priorityExpiryInterval);
await this.prismaService.shelterSupply.updateMany({
data: {
priority: SupplyPriority.Needing,
},
where: {
priority: SupplyPriority.Urgent,
createdAt: {
lte: expiryDate,
},
},
});
}
}
3 changes: 2 additions & 1 deletion src/shelter-supply/shelter-supply.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { Module } from '@nestjs/common';
import { ShelterSupplyService } from './shelter-supply.service';
import { ShelterSupplyController } from './shelter-supply.controller';
import { PrismaModule } from '../prisma/prisma.module';
import { ShelterSupplyExpirationJob } from './shelter-supply-expiration.job';

@Module({
imports: [PrismaModule],
providers: [ShelterSupplyService],
providers: [ShelterSupplyService, ShelterSupplyExpirationJob],
controllers: [ShelterSupplyController],
})
export class ShelterSupplyModule {}
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getSessionData,
deepMerge,
capitalize,
addHours,
} from './utils';

export {
Expand All @@ -12,4 +13,5 @@ export {
removeNotNumbers,
getSessionData,
deepMerge,
addHours,
};
7 changes: 7 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,17 @@ function deepMerge(target: Record<string, any>, source: Record<string, any>) {
}
}

function addHours(date: Date, hours: number) {
const result = new Date();
result.setTime(date.getTime() + hours * 60 * 60 * 1000);
return result;
}

export {
ServerResponse,
removeNotNumbers,
getSessionData,
deepMerge,
capitalize,
addHours,
};