Delete campaign war resources and logs on campaign delete (CC-78)
parent
a3665b5476
commit
82f3dc923f
|
@ -12,9 +12,22 @@ const checkMT = require('../middleware/permission-check').checkMT;
|
||||||
const routerHandling = require('../middleware/router-handling');
|
const routerHandling = require('../middleware/router-handling');
|
||||||
const idValidator = require('../middleware/validators').idValidator;
|
const idValidator = require('../middleware/validators').idValidator;
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const resourceLocation = require('../middleware/resource-location').resourceLocation().concat('/logs/');
|
||||||
|
|
||||||
// Mongoose Model using mongoDB
|
// Mongoose Model using mongoDB
|
||||||
const CampaignModel = require('../models/campaign');
|
const CampaignModel = require('../models/campaign');
|
||||||
const WarModel = require('../models/war');
|
const WarModel = require('../models/war');
|
||||||
|
const PlayerModel = require('../models/player');
|
||||||
|
const LogKillModel = require('../models/logs/kill');
|
||||||
|
const LogVehicleKillModel = require('../models/logs/vehicle');
|
||||||
|
const LogRespawnModel = require('../models/logs/respawn');
|
||||||
|
const LogReviveModel = require('../models/logs/revive');
|
||||||
|
const LogTransportModel = require('../models/logs/transport');
|
||||||
|
const LogFlagModel = require('../models/logs/flag');
|
||||||
|
const LogBudgetModel = require('../models/logs/budget');
|
||||||
|
const LogPointsModel = require('../models/logs/points');
|
||||||
|
const LogPlayerCountModel = require('../models/logs/player-count');
|
||||||
|
|
||||||
// util
|
// util
|
||||||
const genericGetById = require('./_generic').genericGetById;
|
const genericGetById = require('./_generic').genericGetById;
|
||||||
|
@ -88,20 +101,50 @@ campaigns.route('/:id')
|
||||||
})
|
})
|
||||||
|
|
||||||
.delete((req, res, next) => {
|
.delete((req, res, next) => {
|
||||||
CampaignModel.findByIdAndRemove(req.params.id, (err, item) => {
|
WarModel.find({campaign: req.params.id}, (err, wars) => {
|
||||||
if (err) {
|
|
||||||
err.status = codes.wrongrequest;
|
wars.forEach((item) => {
|
||||||
return next(err);
|
// delete linked appearances
|
||||||
} else if (!item) {
|
PlayerModel.find({warId: item._id}).remove().exec();
|
||||||
err = new Error('item not found');
|
LogKillModel.find({war: item._id}).remove().exec();
|
||||||
err.status = codes.notfound;
|
LogVehicleKillModel.find({war: item._id}).remove().exec();
|
||||||
return next(err);
|
LogRespawnModel.find({war: item._id}).remove().exec();
|
||||||
}
|
LogReviveModel.find({war: item._id}).remove().exec();
|
||||||
WarModel.find({campaign: req.params.id}).remove().exec();
|
LogFlagModel.find({war: item._id}).remove().exec();
|
||||||
// TODO: remove all the war logs from fs here!!!
|
LogBudgetModel.find({war: item._id}).remove().exec();
|
||||||
// TODO: remove all LOG entries from DB!!!
|
LogTransportModel.find({war: item._id}).remove().exec();
|
||||||
res.locals.processed = true;
|
LogPointsModel.find({war: item._id}).remove().exec();
|
||||||
next();
|
LogPlayerCountModel.find({war: item._id}).remove().exec();
|
||||||
|
|
||||||
|
// check if logfiles exist and delete from fs
|
||||||
|
const warDir = resourceLocation + item.id;
|
||||||
|
if (fs.existsSync(warDir)) {
|
||||||
|
const cleanLog = warDir + '/clean.log';
|
||||||
|
if (fs.existsSync(cleanLog)) {
|
||||||
|
fs.unlink(cleanLog, (err) => {
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const sourceLog = warDir + '/war.log';
|
||||||
|
if (fs.existsSync(sourceLog)) {
|
||||||
|
fs.unlink(sourceLog, (err) => {
|
||||||
|
});
|
||||||
|
}
|
||||||
|
fs.rmdir(warDir, (err) => {
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
CampaignModel.findByIdAndRemove(req.params.id, (err, item) => {
|
||||||
|
if (err) {
|
||||||
|
err.status = codes.wrongrequest;
|
||||||
|
return next(err);
|
||||||
|
} else if (!item) {
|
||||||
|
err = new Error('item not found');
|
||||||
|
err.status = codes.notfound;
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
res.locals.processed = true;
|
||||||
|
next();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue