Skip to content

Commit

Permalink
Lots of fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
octaviotastico committed Feb 18, 2022
1 parent f2de394 commit de1ee10
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 60 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,7 @@ dist
.tern-port

# Yarn Files
yarn.lock
yarn.lock

# Package-lock.json
package-lock.json
61 changes: 31 additions & 30 deletions lib/aap/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const initializeDTN = async (dtnHost, dtnPort, agentID) => {
const listen = async (netClient) => {
const { deserializeMessage } = serializers;

netClient.on('data', (receivedData) => {
netClient.on('data', async (receivedData) => {
console.log('[INFO] Received data from μD3TN:');

// Update amount of MB received from foreign databases.
Expand Down Expand Up @@ -140,13 +140,13 @@ const updateLocalDatabase = async ({ type, modelName, localDate, data }) => {
} else if (type === 'dtInsertMany') {
model.insertMany(data);
} else if (type === 'dtDeleteOne') {
const { filter, options, callback } = data;
model.deleteOne(filter, options, callback);
const { filter, options } = data;
model.deleteOne(filter, options).exec();
} else if (type === 'dtDeleteMany') {
const { filter, options, callback } = data;
model.deleteMany(filter, options, callback);
const { filter, options } = data;
model.deleteMany(filter, options).exec();
} else if (type === 'dtUpdateOne') {
const { filter, update, options, callback } = data;
const { filter, update, options } = data;
model.findOne(filter, (err, doc) => {
if (err) {
console.log('[ERROR] dtUpdateOne - Failed to find document:', err);
Expand All @@ -157,28 +157,28 @@ const updateLocalDatabase = async ({ type, modelName, localDate, data }) => {
const localUpdatedAt = doc && doc.updatedAt && new Date(doc.updatedAt);
const isValidDate = localUpdatedAt instanceof Date && !isNaN(localUpdatedAt);
if (!isValidDate || (localUpdatedAt < receivedUpdatedAt)) {
model.updateOne(filter, update, options, callback);
console.log('[INFO] Received localDate is newer than the one in the database, not updating.');
model.updateOne(filter, update, options).exec();
console.log('[INFO] Received localDate is newer than the one in the database, updating the database.');
return;
}
});
} else if (type === 'dtUpdateMany') {
const { filter, update, options, callback } = data;
model.updateMany(filter, update, options, callback);
const { filter, update, options } = data;
model.updateMany(filter, update, options).exec();
} else if (type === 'dtReplaceOne') {
const { filter, replacement, options, callback } = data;
model.replaceOne(filter, replacement, options, callback);
const { filter, replacement, options } = data;
model.replaceOne(filter, replacement, options).exec();
} else if (type === 'dtFindOneAndDelete') {
const { filter, options, callback } = data;
model.findOneAndDelete(filter, options, callback);
const { filter, options } = data;
model.findOneAndDelete(filter, options).exec();
} else if (type === 'dtFindOneAndRemove') {
const { filter, options, callback } = data;
model.findOneAndRemove(filter, options, callback);
const { filter, options } = data;
model.findOneAndRemove(filter, options).exec();
} else if (type === 'dtFindOneAndReplace') {
const { filter, replacement, options, callback } = data;
model.findOneAndReplace(filter, replacement, options, callback);
const { filter, replacement, options } = data;
model.findOneAndReplace(filter, replacement, options).exec();
} else if (type === 'dtFindOneAndUpdate') {
const { filter, update, options, callback } = data;
const { filter, update, options } = data;
model.findOne(filter, (err, doc) => {
if (err) {
console.log('[ERROR] dtFindOneAndUpdate - Failed to find document:', err);
Expand All @@ -189,31 +189,32 @@ const updateLocalDatabase = async ({ type, modelName, localDate, data }) => {
const localUpdatedAt = doc && doc.updatedAt && new Date(doc.updatedAt);
const isValidDate = localUpdatedAt instanceof Date && !isNaN(localUpdatedAt);
if (!isValidDate || (localUpdatedAt < receivedUpdatedAt)) {
model.findOneAndUpdate(filter, update, options, callback);
console.log('[INFO] Received localDate is newer than the one in the database, not updating.');
model.findOneAndUpdate(filter, update, options).exec();
console.log('[INFO] Received localDate is newer than the one in the database, updating the database.');
return;
}
});
} else if (type === 'dtFindByIdAndDelete') {
const { id, options, callback } = data;
model.findByIdAndDelete(id, options, callback);
const { id, options } = data;
model.findByIdAndDelete(id, options).exec();
} else if (type === 'dtFindByIdAndRemove') {
const { id, options, callback } = data;
model.findByIdAndRemove(id, options, callback);
const { id, options } = data;
model.findByIdAndRemove(id, options).exec();
} else if (type === 'dtFindByIdAndUpdate') {
const { id, update, options, callback } = data;
model.findById(id, (err, doc) => {
const { id, update, options } = data;
model.findById(id._id, (err, doc) => {
if (err) {
console.log('[ERROR] dtFindByIdAndUpdate - Failed to find document:', err);
console.log('[ERROR] dtFindByIdAndUpdate - Failed to find document:', id._id, err);
return;
}
console.log('[INFO] dtFindByIdAndUpdate - Found document:', id._id);
// If received localDate is newer than the one in the database, update the database.
const receivedUpdatedAt = new Date(localDate);
const localUpdatedAt = doc && doc.updatedAt && new Date(doc.updatedAt);
const isValidDate = localUpdatedAt instanceof Date && !isNaN(localUpdatedAt);
if (!isValidDate || (localUpdatedAt < receivedUpdatedAt)) {
model.findByIdAndUpdate(id, update, options, callback);
console.log('[INFO] Received localDate is newer than the one in the database, not updating.');
model.findByIdAndUpdate(id._id, update, options).exec();
console.log('[INFO] Received localDate is newer than the one in the database, updating the database.');
return;
}
});
Expand Down
Loading

0 comments on commit de1ee10

Please sign in to comment.