Skip to content

Commit

Permalink
Adding returns back in (#63)
Browse files Browse the repository at this point in the history
* adding returns back in. Two more API's to handle.

* final bits on handling errors

* fixed create execution test and cleanup which was removing all executions

* disabling test to check pipeline.
  • Loading branch information
snevesbarros authored Mar 18, 2023
1 parent 1c3c6b5 commit ce931cf
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 347 deletions.
8 changes: 5 additions & 3 deletions app/controllers/angles.controller.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const { validationResult } = require('express-validator');
const debug = require('debug');
const mongoose = require('mongoose');
// eslint-disable-next-line import/extensions
const { version } = require('../../package.json');
const { handleError } = require('../exceptions/errors.js');

// eslint-disable-next-line no-unused-vars
const log = debug('angles:controller');

exports.versions = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
return mongoose.connection.db.command({ buildInfo: 1 })
.then((info) => {
Expand All @@ -18,8 +20,8 @@ exports.versions = (req, res) => {
mongo: info.version,
angles: version,
};
res.status(200).send(response);
return res.status(200).send(response);
}).catch((err) => {
handleError(err, res);
return handleError(err, res);
});
};
44 changes: 20 additions & 24 deletions app/controllers/baseline.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.create = (req, res) => {
// check the request is valid
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}

const { screenshotId, view: requestView, ignoreBoxes } = req.body;
Expand All @@ -27,7 +27,7 @@ exports.create = (req, res) => {
Screenshot.findById(screenshotId).exec(),
Baseline.find({ view: requestView }).exec(),
];
Promise.all(promises)
return Promise.all(promises)
.then((results) => {
const screenshot = results[0];
const baselinesFound = results[1];
Expand Down Expand Up @@ -64,16 +64,14 @@ exports.create = (req, res) => {
})
.then((savedBaseline) => {
log(`Created baseline with id "${savedBaseline._id}" for view "${savedBaseline.view}" and platorm "${savedBaseline.platformName}"`);
res.status(201).send(savedBaseline);
}).catch((err) => {
handleError(err, res);
});
return res.status(201).send(savedBaseline);
}).catch((err) => handleError(err, res));
};

exports.findAll = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const {
view,
Expand All @@ -91,37 +89,35 @@ exports.findAll = (req, res) => {
if (browserName) baseLineQuery['platform.browserName'] = browserName;
if (screenHeight) baseLineQuery.screenHeight = screenHeight;
if (screenWidth) baseLineQuery.screenWidth = screenWidth;
Baseline.find(baseLineQuery)
return Baseline.find(baseLineQuery)
.populate('screenshot')
.then((baselines) => {
res.status(200).send(baselines);
return res.status(200).send(baselines);
})
.catch((err) => {
handleError(err, res);
return handleError(err, res);
});
};

exports.findOne = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const { baselineId } = req.query;
Baseline.findById(baselineId)
return Baseline.findById(baselineId)
.then((baseline) => {
if (!baseline) {
throw new NotFoundError(`Baseline not found with id ${baselineId}`);
}
res.status(200).send(baseline);
}).catch((err) => {
handleError(err, res);
});
return res.status(200).send(baseline);
}).catch((err) => handleError(err, res));
};

exports.update = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const { screenshotId, ignoreBoxes } = req.body;
let screenshotPromise = new Promise((resolve) => { resolve(); });
Expand All @@ -134,7 +130,7 @@ exports.update = (req, res) => {
screenshotPromise,
Baseline.findById(baselineId).exec(),
];
Promise.all(promises)
return Promise.all(promises)
.then((results) => {
const screenshot = results[0];
const baselineFound = results[1];
Expand All @@ -158,26 +154,26 @@ exports.update = (req, res) => {
})
.then((savedBaseline) => savedBaseline.populate('screenshot'))
.then((savedBaselineWithScreenshot) => {
res.status(200).send(savedBaselineWithScreenshot);
return res.status(200).send(savedBaselineWithScreenshot);
})
.catch((err) => {
handleError(err, res);
return handleError(err, res);
});
};

exports.delete = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const { baselineId } = req.params;
Baseline.findByIdAndRemove(baselineId)
return Baseline.findByIdAndRemove(baselineId)
.then((baseline) => {
if (!baseline) {
throw new NotFoundError(`Baseline not found with id ${baselineId}`);
}
res.status(200).send({ message: 'Baseline deleted successfully!' });
return res.status(200).send({ message: 'Baseline deleted successfully!' });
}).catch((err) => {
handleError(err, res);
return handleError(err, res);
});
};
90 changes: 36 additions & 54 deletions app/controllers/build.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const log = debug('build:controller');
exports.create = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const {
team,
Expand All @@ -39,7 +39,7 @@ exports.create = (req, res) => {
phasePromise,
];

Promise.all(promises)
return Promise.all(promises)
.then((results) => {
const teamFound = results[0];
const environmentFound = results[1];
Expand Down Expand Up @@ -92,17 +92,15 @@ exports.create = (req, res) => {
.exec())
.then((savedBuild) => {
log(`Created build "${savedBuild.name}" for team "${savedBuild.team.name}" with id: ${savedBuild._id}`);
res.status(201).send(savedBuild);
return res.status(201).send(savedBuild);
})
.catch((err) => {
handleError(err, res);
});
.catch((err) => handleError(err, res));
};

exports.findAll = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const {
teamId,
Expand All @@ -116,7 +114,7 @@ exports.findAll = (req, res) => {
const limit = parseInt(req.query.limit, 10) || 10;
const skip = parseInt(req.query.skip, 10) || 0;
let query = {};
Team.findById({ _id: teamId })
return Team.findById({ _id: teamId })
.then((teamFound) => {
if (!teamFound) {
throw new NotFoundError(`No team found with name ${req.body.team}`);
Expand Down Expand Up @@ -170,20 +168,18 @@ exports.findAll = (req, res) => {
const builds = results[0];
const count = results[1];
const response = { builds, count };
res.status(200).send(response);
return res.status(200).send(response);
})
.catch((err) => {
handleError(err, res);
});
.catch((err) => handleError(err, res));
};

exports.findOne = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const { buildId } = req.params;
Build.findById(buildId)
return Build.findById(buildId)
.populate('team')
.populate('environment')
.populate('phase')
Expand All @@ -192,21 +188,19 @@ exports.findOne = (req, res) => {
if (!build) {
throw new NotFoundError(`No build found with id ${buildId}`);
}
res.status(200).send(build);
return res.status(200).send(build);
})
.catch((err) => {
handleError(err, res);
});
.catch((err) => handleError(err, res));
};

exports.getReport = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
let build;
const { buildId } = req.params;
Build.findById(req.params.buildId)
return Build.findById(req.params.buildId)
.populate('team')
.populate('environment')
.populate('phase')
Expand All @@ -222,62 +216,56 @@ exports.getReport = (req, res) => {
})
.then((screenshots) => {
// eslint-disable-next-line global-require
res.render('index', { build, screenshots, moment: require('moment') });
return res.render('index', { build, screenshots, moment: require('moment') });
})
.catch((err) => {
handleError(err, res);
});
.catch((err) => handleError(err, res));
};

// TODO: We should be able to update more than just team and/or environment.
exports.update = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const { buildId } = req.params;
const { team, environment } = req.body;
Build.findByIdAndUpdate(buildId, {
return Build.findByIdAndUpdate(buildId, {
team,
environment,
}, { new: true })
.then((build) => {
if (!build) {
throw new NotFoundError(`No build found with id ${buildId}`);
}
res.status(200).send(build);
}).catch((err) => {
handleError(err, res);
});
return res.status(200).send(build);
}).catch((err) => handleError(err, res));
};

exports.setKeep = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const { buildId } = req.params;
const { keep } = req.body;
Build.findByIdAndUpdate(buildId, {
return Build.findByIdAndUpdate(buildId, {
keep,
}, { new: true })
.then((build) => {
if (!build) {
throw new NotFoundError(`No build found with id ${buildId}`);
}
res.status(200).send(build);
}).catch((err) => {
handleError(err, res);
});
return res.status(200).send(build);
}).catch((err) => handleError(err, res));
};

exports.setArtifacts = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const { buildId } = req.params;
Build.findByIdAndUpdate(buildId, {
return Build.findByIdAndUpdate(buildId, {
artifacts: req.body.artifacts,
}, { new: true })
.populate('team')
Expand All @@ -287,11 +275,9 @@ exports.setArtifacts = (req, res) => {
if (!build) {
throw new NotFoundError(`No build found with id ${buildId}`);
}
res.status(200).send(build);
return res.status(200).send(build);
})
.catch((err) => {
handleError(err, res);
});
.catch((err) => handleError(err, res));
};

/*
Expand All @@ -303,30 +289,28 @@ exports.setArtifacts = (req, res) => {
exports.delete = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
const { buildId } = req.params;
Build.findByIdAndRemove(buildId)
return Build.findByIdAndRemove(buildId)
.then((build) => {
if (!build) {
throw new NotFoundError(`No build found with id ${buildId}`);
}
res.status(200).send({ message: 'Build deleted successfully!' });
}).catch((err) => {
handleError(err, res);
});
return res.status(200).send({ message: 'Build deleted successfully!' });
}).catch((err) => handleError(err, res));
};

exports.deleteMany = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
return res.status(422).json({ errors: errors.array() });
}
let allBuildsToDelete;
let reportingMetrics = {};
const { teamId, ageInDays } = req.query;
// delete by team and age (or default age 2 months) unless keep flag
Team.findById({ _id: teamId })
return Team.findById({ _id: teamId })
.then((teamFound) => {
if (!teamFound) {
throw new NotFoundError(`No team found with id ${teamId}`);
Expand Down Expand Up @@ -373,9 +357,7 @@ exports.deleteMany = (req, res) => {
})
.then((results) => {
log(results);
res.status(200).send({ message: `Deleted [${reportingMetrics.buildsToDeleteLength}] for team with id ${teamId} and age ${ageInDays}. Unable to delete ${reportingMetrics.uniqueBaselineBuildIdsLength} builds as they have baselines set.` });
return res.status(200).send({ message: `Deleted [${reportingMetrics.buildsToDeleteLength}] for team with id ${teamId} and age ${ageInDays}. Unable to delete ${reportingMetrics.uniqueBaselineBuildIdsLength} builds as they have baselines set.` });
})
.catch((err) => {
handleError(err, res);
});
.catch((err) => handleError(err, res));
};
Loading

0 comments on commit ce931cf

Please sign in to comment.