Skip to content

Commit

Permalink
fix schedules bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Asing1001 committed Aug 26, 2017
1 parent de6c163 commit 4693261
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ app.get('/api/ticketstatus', (req, res, next) => {
redisClient.get(ticketStatusKey, async (err, strTicketStatus) => {
let ticketStatus;
if (!strTicketStatus) {
ticketStatus = await getAllTicketStatusWithSchedule();
ticketStatus = await getAllTicketStatus();
ticketStatus = await mergeTicketSchedule(ticketStatus)
redisClient.set(ticketStatusKey, JSON.stringify(ticketStatus))
} else {
ticketStatus = JSON.parse(strTicketStatus);
Expand All @@ -22,16 +23,18 @@ app.get('/api/ticketstatus', (req, res, next) => {
})
})

const getAllTicketStatusWithSchedule = async () => {
const mergeTicketSchedule = async (ticketStatus) => {
return new Promise(async (resolve) => {
let ticketStatus = await getAllTicketStatus();
if (ticketStatus.length > 0) {
redisClient.get(scheduleKey, async (err, strSchedule) => {
const schedules = JSON.parse(strSchedule) || [];
ticketStatus = ticketStatus.map(t => {
t.schedules = schedules
.filter(({ date, sport, place }) => t.date === date && t.sport === sport && t.place === place)
.map(({ item, gender, stage, time }) => ({ item, gender, stage, time }));
if (t.schedules.length === 0) {
console.error('Schedules not found!, ticket:', JSON.stringify(t))
}
return t;
})
resolve(ticketStatus)
Expand All @@ -57,12 +60,13 @@ app.listen(port, () => {

setInterval(async () => {
console.time('[Scheduler] getAllTicketStatus');
const newTicketStatus = await getAllTicketStatusWithSchedule();
const newTicketStatus = await getAllTicketStatus();
redisClient.get(ticketStatusKey, async (err, strTicketStatus) => {
const oldTicketStatus = JSON.parse(strTicketStatus);
let ticketStatus;
if (oldTicketStatus) {
ticketStatus = mergeTicketStatus(oldTicketStatus, newTicketStatus);
ticketStatus = await mergeTicketSchedule(ticketStatus)
} else {
ticketStatus = newTicketStatus;
}
Expand Down

0 comments on commit 4693261

Please sign in to comment.