-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from yeha98555/feature/delete-expired-order
Feature/delete-expired-order
- Loading branch information
Showing
16 changed files
with
70 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Redis } from 'ioredis'; | ||
|
||
export const redis = new Redis(process.env.REDIS_CONNECT_STRING); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export enum OrderStatus { | ||
TEMP, | ||
PENDING, | ||
SUCCESS, | ||
CANCELLED, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Redis } from 'ioredis'; | ||
import { redis } from '@/connections/redis'; | ||
import OrderModel from '@/models/order'; | ||
import { OrderStatus } from '@/enums/orderStatus'; | ||
import SeatReservationModel from '@/models/seatReservation'; | ||
|
||
const sub = new Redis(redis.options); | ||
const redisDB = redis.options.db; | ||
|
||
const channel = `__keyevent@${redisDB}__:expired`; | ||
export const key = (orderId: string) => `order-expire-checking-${orderId}`; | ||
|
||
const isOrderExpiredEvent = (key: string) => | ||
key.startsWith('order-expire-checking-'); | ||
const getOrderId = (key: string) => key.replace('order-expire-checking-', ''); | ||
|
||
sub.subscribe(channel, (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
|
||
sub.on('message', (channel, key) => { | ||
isOrderExpiredEvent(key) && onOrderExpire(getOrderId(key)); | ||
}); | ||
|
||
async function onOrderExpire(orderId: string) { | ||
const order = await OrderModel.findById(orderId); | ||
if (!order || order.status !== OrderStatus.PENDING) return; | ||
|
||
await SeatReservationModel.findByIdAndDelete(order.seat_reservation_id); | ||
await order.deleteOne(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters