Skip to content

Commit 5cab976

Browse files
committed
Added logic to change priority for outdated supplies with priority urgent
1 parent 2895265 commit 5cab976

File tree

6 files changed

+81
-4
lines changed

6 files changed

+81
-4
lines changed

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 };

src/shelter-supply/shelter-supply.service.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { z } from 'zod';
21
import { Injectable } from '@nestjs/common';
2+
import { z } from 'zod';
33

44
import { PrismaService } from '../prisma/prisma.service';
5+
import { SupplyPriority } from '../supply/types';
6+
import { getDifferenceInHours } from '../utils';
7+
import { priorityExpiryInterval } from './constants';
58
import { CreateShelterSupplySchema, UpdateShelterSupplySchema } from './types';
69

710
@Injectable()
@@ -98,4 +101,23 @@ export class ShelterSupplyService {
98101
},
99102
});
100103
}
104+
105+
async checkAndUpdateOutdatedPriorities(shelterSupplies: Array<any>) {
106+
shelterSupplies.forEach((s) => {
107+
if (
108+
s.priority === SupplyPriority.Urgent &&
109+
getDifferenceInHours(
110+
new Date(s.updatedAt || s.createdAt),
111+
new Date(Date.now()),
112+
) >= priorityExpiryInterval
113+
) {
114+
this.update({
115+
data: {
116+
priority: SupplyPriority.Needing,
117+
},
118+
where: { shelterId: s.shelterId, supplyId: s.supply.id },
119+
});
120+
}
121+
});
122+
}
101123
}

src/shelter/shelter.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 { ShelterService } from './shelter.service';
44
import { ShelterController } from './shelter.controller';
55
import { PrismaModule } from '../prisma/prisma.module';
6+
import { ShelterSupplyService } from '../shelter-supply/shelter-supply.service';
67

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

src/shelter/shelter.service.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ import {
1111
} from './types';
1212
import { SeachQueryProps } from '@/decorators/search-query/types';
1313
import { SupplyPriority } from '../supply/types';
14+
import { ShelterSupplyService } from 'src/shelter-supply/shelter-supply.service';
1415

1516
@Injectable()
1617
export class ShelterService {
17-
constructor(private readonly prismaService: PrismaService) {}
18+
constructor(
19+
private readonly prismaService: PrismaService,
20+
private readonly shelterSupplyService: ShelterSupplyService,
21+
) {}
1822

1923
async store(body: z.infer<typeof CreateShelterSchema>) {
2024
const payload = CreateShelterSchema.parse(body);
@@ -102,7 +106,40 @@ export class ShelterService {
102106
async index(props: SeachQueryProps) {
103107
const { handleSearch } = props;
104108

105-
return await handleSearch<Prisma.ShelterSelect<DefaultArgs>>(
109+
const partialResult = await handleSearch<Prisma.ShelterSelect<DefaultArgs>>(
110+
this.prismaService.shelter,
111+
{
112+
select: {
113+
id: true,
114+
shelterSupplies: {
115+
where: {
116+
priority: {
117+
gte: SupplyPriority.Urgent,
118+
},
119+
},
120+
take: 10,
121+
select: {
122+
priority: true,
123+
supply: {
124+
select: {
125+
id: true,
126+
},
127+
},
128+
createdAt: true,
129+
updatedAt: true,
130+
},
131+
},
132+
},
133+
},
134+
);
135+
136+
await this.shelterSupplyService.checkAndUpdateOutdatedPriorities(
137+
partialResult.results.flatMap((r) =>
138+
r.shelterSupplies.map((s) => ({ ...s, shelterId: r.id })),
139+
),
140+
);
141+
142+
const result = await handleSearch<Prisma.ShelterSelect<DefaultArgs>>(
106143
this.prismaService.shelter,
107144
{
108145
select: {
@@ -142,5 +179,7 @@ export class ShelterService {
142179
},
143180
},
144181
);
182+
183+
return result;
145184
}
146185
}

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+
getDifferenceInHours,
910
} from './utils';
1011

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

src/utils/utils.ts

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

143+
function getDifferenceInHours(startDate: Date, endDate: Date) {
144+
const timeDiff = endDate.getTime() - startDate.getTime();
145+
return getHours(timeDiff);
146+
}
147+
148+
function getHours(date: number) {
149+
return date / (1000 * 60 * 60);
150+
}
151+
143152
export {
144153
ServerResponse,
145154
removeNotNumbers,
@@ -148,4 +157,5 @@ export {
148157
parseStringToObject,
149158
getSearchWhere,
150159
capitalize,
160+
getDifferenceInHours,
151161
};

0 commit comments

Comments
 (0)