Skip to content

Commit

Permalink
Merge pull request #27 from CMU-313/dominic-p1-changes
Browse files Browse the repository at this point in the history
Refactoring code in src/upgrades/1.0.0/chat_room_hashes.js
  • Loading branch information
dominicteh1 authored Sep 12, 2024
2 parents 06b21d8 + 9628970 commit 05afb1a
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/upgrades/1.0.0/chat_room_hashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,36 @@ module.exports = {
if (err) {
return callback(err);
}
let currentChatRoomId = 1;
async.whilst((next) => {
next(null, currentChatRoomId <= nextChatRoomId);
}, (next) => {
db.getSortedSetRange(`chat:room:${currentChatRoomId}:uids`, 0, 0, (err, uids) => {
const currentChatRoomId = 1;
async.whilst(
(next) => {
next(null, currentChatRoomId <= nextChatRoomId);
},
(next) => {
processChatRoom(currentChatRoomId, next);
},
callback
);
});

function processChatRoom(currentChatRoomId, next) {
db.getSortedSetRange(`chat:room:${currentChatRoomId}:uids`, 0, 0, (err, uids) => {
if (err) {
return next(err);
}
if (!Array.isArray(uids) || !uids.length || !uids[0]) {
currentChatRoomId += 1;
return next();
}

db.setObject(`chat:room:${currentChatRoomId}`, { owner: uids[0], roomId: currentChatRoomId }, (err) => {
if (err) {
return next(err);
}
if (!Array.isArray(uids) || !uids.length || !uids[0]) {
currentChatRoomId += 1;
return next();
}

db.setObject(`chat:room:${currentChatRoomId}`, { owner: uids[0], roomId: currentChatRoomId }, (err) => {
if (err) {
return next(err);
}
currentChatRoomId += 1;
next();
});
currentChatRoomId += 1;
next();
});
}, callback);
});
});
}
},
};

0 comments on commit 05afb1a

Please sign in to comment.