Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit e1df1d9

Browse files
committed
Fixing duplicate records for hostiles on submission.
1 parent 39c3cde commit e1df1d9

3 files changed

Lines changed: 33 additions & 30 deletions

File tree

lib/actions/postStatusAction.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function (hostileService, eventService, memberService, standingsService, neow, headerParser, errorResponse, successResponse, _) {
1+
module.exports = function (hostileService, eventService, memberService, standingsService, neow, headerParser, errorResponse, successResponse, async, _) {
22

33
var pub = {};
44

@@ -66,13 +66,36 @@ module.exports = function (hostileService, eventService, memberService, standing
6666
});
6767

6868
} else if (scanData.text === 'hostile') {
69-
hostileService.addAndGet(headerParser.parse(req), scanData, req.session.armadaKey, function (error, status) {
70-
if (error) return errorResponse.respond(res, 'status', 'Unable to add status.');
69+
var hostiles = [];
70+
71+
async.each(scanData.pilots, function(pilot, callback) {
72+
async.waterfall([
73+
function(callback){
74+
hostileService.getById(pilot.id, function(error, result) {
75+
if (error) return errorResponse.respond(res, 'status', 'Unable to find hostile by id ' + pilot.id);
76+
if (result !== null) {
77+
pilot.key = result.key;
78+
if (result.shipType !== null) pilot.shipType = result.shipType;
79+
if (result.shipTypeId !== null) pilot.shipTypeId = result.shipTypeId;
80+
}
81+
callback(null, pilot);
82+
});
83+
},
84+
function(pilot, callback){
85+
hostileService.updateAndGet(headerParser.parse(req), pilot, req.session.armadaKey, function(hostile) {
86+
callback(null, hostile);
87+
});
88+
}
89+
], function (error, hostile) {
90+
if (error) return errorResponse.respond(res, 'status', 'Unable to parse status.');
91+
hostiles.push(hostile);
92+
callback();
93+
});
7194

95+
}, function(error) {
7296
memberService.getByKey(req.session.memberKey, function (error, member) {
7397
if (error) return errorResponse.respond(res, 'member', 'Error fetching member');
74-
75-
eventService.addAndGet('reportHostile', status, req.session.armadaKey, function (error, event) {
98+
eventService.addAndGet('reportHostile', hostiles, req.session.armadaKey, function(error, event) {
7699
successResponse.respond(res);
77100
});
78101
});

lib/services/hostileService.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function (keyGenerator, storageManager, settings, Q) {
1+
module.exports = function (keyGenerator, storageManager, settings) {
22

33
var pub = {};
44

@@ -41,29 +41,7 @@ module.exports = function (keyGenerator, storageManager, settings, Q) {
4141
};
4242

4343
pub.addAndGet = function(headerData, scanData, armadaKey, callback) {
44-
var hostiles = [];
45-
46-
for (i in scanData.pilots) {
47-
48-
storageManager.getById('hostile', scanData.pilots[i].id, function(error, result) {
49-
if (error) return callback(error);
50-
var hostile = createHostile(headerData, scanData.pilots[i], armadaKey);
51-
52-
if (result !== null) hostile.key = result.key;
53-
debugger;
54-
55-
return hostile;
56-
}).then(function(hostile) {
57-
debugger;
58-
storageManager.updateItem('hostile', hostile.key, hostile, function (error) {
59-
if (error) return callback(error);
60-
});
61-
62-
hostiles.push(hostile);
63-
});
64-
}
65-
66-
callback(null, hostiles);
44+
hostileService.updateAndGet(headerData, scanData, armadaKey, callback);
6745
};
6846

6947
pub.updateAndGet = function(headerData, scanData, armadaKey, callback) {
@@ -73,7 +51,7 @@ module.exports = function (keyGenerator, storageManager, settings, Q) {
7351
if (error) return callback(error);
7452
});
7553

76-
callback(null, hostile);
54+
callback(hostile);
7755
};
7856

7957
pub.getById = function (id, callback) {

lib/storage/cacheLayer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ module.exports = function (logger, _) {
6666
};
6767

6868
pub.upsertItem = function (type, item) {
69+
if (item === null) return;
70+
6971
// logger.log('Updating ' + type + ' ' + item.key + ' in cache...', 2);
7072
for (var i in store[type]) {
7173
if (store[type][i].key === item.key) {

0 commit comments

Comments
 (0)