Claim availability check #434

Merged
bones7242 merged 7 commits from claim-availability-check into master 2018-05-06 20:22:38 +02:00
8 changed files with 61 additions and 24 deletions

View file

@ -3,13 +3,12 @@ const db = require('../../../../models');
const checkChannelAvailability = (name) => { const checkChannelAvailability = (name) => {
return db.Channel return db.Channel
.findAll({ .findAll({
where: { channelName: name }, where: {
channelName: name,
},
}) })
.then(result => { .then(result => {
if (result.length >= 1) { return (result.length <= 0);
throw new Error('That channel has already been claimed');
}
return name;
}) })
.catch(error => { .catch(error => {
throw error; throw error;

View file

@ -8,12 +8,29 @@ const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
*/ */
function addAtSymbolIfNecessary (name) {
if (name.substring(0,1) !== '@') {
return `@${name}`;
}
return name;
}
const channelAvailability = ({ ip, originalUrl, params: { name } }, res) => { const channelAvailability = ({ ip, originalUrl, params: { name } }, res) => {
const gaStartTime = Date.now(); const gaStartTime = Date.now();
name = addAtSymbolIfNecessary(name);
checkChannelAvailability(name) checkChannelAvailability(name)
.then(availableName => { .then(isAvailable => {
res.status(200).json(availableName); let responseObject = {
sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now()); success: true,
data: isAvailable,
};
if (isAvailable) {
responseObject['message'] = `${name} is available`
} else {
responseObject['message'] = `${name} is already in use`
}
res.status(200).json(responseObject);
sendGATimingEvent('end-to-end', 'channel name availability', name, gaStartTime, Date.now());
}) })
.catch(error => { .catch(error => {
handleErrorResponse(originalUrl, ip, error, res); handleErrorResponse(originalUrl, ip, error, res);

View file

@ -1,4 +1,4 @@
const db = require('../../../../models/index'); const db = require('../../../../models');
const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../../../../config/siteConfig.js'); const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../../../../config/siteConfig.js');
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
const Op = Sequelize.Op; const Op = Sequelize.Op;
@ -18,10 +18,7 @@ const claimAvailability = (name) => {
}, },
}) })
.then(result => { .then(result => {
if (result.length >= 1) { return (result.length <= 0);
throw new Error('That claim is already in use');
}
return name;
}) })
.catch(error => { .catch(error => {
throw error; throw error;

View file

@ -11,8 +11,17 @@ const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const claimAvailability = ({ ip, originalUrl, params: { name } }, res) => { const claimAvailability = ({ ip, originalUrl, params: { name } }, res) => {
const gaStartTime = Date.now(); const gaStartTime = Date.now();
checkClaimAvailability(name) checkClaimAvailability(name)
.then(result => { .then(isAvailable => {
res.status(200).json(result); let responseObject = {
success: true,
data: isAvailable,
};
if (isAvailable) {
responseObject['message'] = `That claim name is available`
} else {
responseObject['message'] = `That url is already in use`
}
res.status(200).json(responseObject);
sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now()); sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now());
}) })
.catch(error => { .catch(error => {

View file

@ -14,9 +14,15 @@ const claimData = ({ ip, originalUrl, body, params }, res) => {
db.Claim.resolveClaim(claimName, claimId) db.Claim.resolveClaim(claimName, claimId)
.then(claimInfo => { .then(claimInfo => {
if (!claimInfo) { if (!claimInfo) {
return res.status(404).json({success: false, message: 'No claim could be found'}); return res.status(404).json({
success: false,
message: 'No claim could be found'
});
} }
res.status(200).json({success: true, data: claimInfo}); res.status(200).json({
success: true,
data: claimInfo
});
}) })
.catch(error => { .catch(error => {
handleErrorResponse(originalUrl, ip, error, res); handleErrorResponse(originalUrl, ip, error, res);

View file

@ -8,7 +8,12 @@ const serveFile = ({ filePath, fileType }, res) => {
'Content-Type' : fileType || 'image/jpeg', 'Content-Type' : fileType || 'image/jpeg',
}, },
}; };
res.status(200).sendFile(filePath, sendFileOptions); res.status(200).sendFile(filePath, sendFileOptions, (error) => {
if (error) {
logger.warn(error);
res.status(404).send();
}
});
}; };
module.exports = serveFile; module.exports = serveFile;

View file

@ -1,6 +1,10 @@
const logout = (req, res) => { const logout = (req, res) => {
req.logout(); req.logout();
res.status(200).json({success: true, message: 'you successfully logged out'}); const responseObject = {
success: true,
message: 'you successfully logged out'
};
res.status(200).json(responseObject);
}; };
module.exports = logout; module.exports = logout;

View file

@ -1,9 +1,9 @@
const user = (req, res) => { const user = (req, res) => {
if (req.user) { const responseObject = {
res.status(200).json({success: true, data: req.user}); success: true,
} else { data: req.user,
res.status(401).json({success: false, message: 'user is not logged in'}); };
} res.status(200).json(responseObject);
}; };
module.exports = user; module.exports = user;