Reworked routes to work with new models
This commit is contained in:
parent
bc324d2488
commit
abbab50366
14 changed files with 31 additions and 36 deletions
|
|
@ -39,7 +39,7 @@ async function CreateUser(req, res, next) {
|
|||
|
||||
const { orgID, ...filteredBody } = body;
|
||||
|
||||
const result = await org.createOrgMember({
|
||||
const result = await org.createMember({
|
||||
...filteredBody,
|
||||
password: passwordHash,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
async function GetMemberOrgs(req, res, next) {
|
||||
const user = req.user;
|
||||
|
||||
const result = await user.getMemberOrgs({
|
||||
const result = await user.getMemberOrg({
|
||||
attributes: { exclude: ['updatedAt'] },
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@ async function GetUserDetails(req, res, next) {
|
|||
const user = req.user;
|
||||
const userID = req.params.userID;
|
||||
|
||||
if (user.userID == userID) {
|
||||
if (user.id == userID) {
|
||||
return res.send(user);
|
||||
}
|
||||
|
||||
const result = await User.findOne({
|
||||
where: { userID: userID },
|
||||
where: { id: userID },
|
||||
attributes: { exclude: ['password', 'createdAt', 'updatedAt'] },
|
||||
});
|
||||
return res.send(result);
|
||||
|
|
|
|||
|
|
@ -4,13 +4,12 @@ async function Login(req, res, next) {
|
|||
const user = req.user;
|
||||
const remember = req.body.remember;
|
||||
|
||||
await Token.destroy({ where: { userID: req.user.userID } });
|
||||
const newToken = await Token.create({
|
||||
userID: req.user.userID,
|
||||
await Token.destroy({ where: { ownerID: user.id } });
|
||||
const newToken = await user.createToken({
|
||||
expires: remember,
|
||||
});
|
||||
|
||||
return res.send({ userID: user.userID, token: newToken.tokenID });
|
||||
return res.send({ userID: user.id, token: newToken.id });
|
||||
}
|
||||
|
||||
module.exports = Login;
|
||||
|
|
|
|||
|
|
@ -19,9 +19,7 @@ async function RequestReset(req, res, next) {
|
|||
authorizationToken: process.env.COURIER_AUTH_TOKEN,
|
||||
});
|
||||
|
||||
const resetRequest = await ResetRequest.create({
|
||||
userID: result.userID,
|
||||
});
|
||||
const resetRequest = await result.createResetRequest();
|
||||
|
||||
await courier.send({
|
||||
message: {
|
||||
|
|
@ -29,7 +27,7 @@ async function RequestReset(req, res, next) {
|
|||
template: 'FSTM4WF60KM570KS9DBQRH0T41GJ',
|
||||
data: {
|
||||
email: result.email,
|
||||
resetRequest: resetRequest.reqID,
|
||||
resetRequest: resetRequest.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@ const forge = require('node-forge');
|
|||
const { ResetRequest } = require('../../db/models/index');
|
||||
|
||||
async function ResetPassword(req, res, next) {
|
||||
reqID = req.params.id;
|
||||
password = req.body.password;
|
||||
const reqID = req.params.id;
|
||||
const password = req.body.password;
|
||||
|
||||
if (!password) {
|
||||
return res.status(400).send('Form missing required information.');
|
||||
}
|
||||
|
||||
const result = await ResetRequest.findOne({ where: { reqID: reqID } });
|
||||
const result = await ResetRequest.findByPk(reqID);
|
||||
|
||||
if (!result) {
|
||||
return res
|
||||
|
|
|
|||
|
|
@ -6,13 +6,11 @@ function SetAudio(req, res, next) {
|
|||
const oldLocation = req.files.audioFile.filepath;
|
||||
|
||||
const uploadDir = `${__dirname}/../../public/audio`;
|
||||
const fileName = `${user.userID}.mp3`;
|
||||
const fileName = `${user.id}.mp3`;
|
||||
const newLocation = `${uploadDir}/${fileName}`;
|
||||
|
||||
fs.rename(oldLocation, newLocation, function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
fs.rename(oldLocation, newLocation, (err) => {
|
||||
if (err) console.log(err);
|
||||
});
|
||||
|
||||
res.send('File saved');
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ async function UpdateUserDetails(req, res, next) {
|
|||
where: {
|
||||
[Op.and]: {
|
||||
email: body.email,
|
||||
userID: { [Op.not]: req.user.userID },
|
||||
id: { [Op.not]: req.user.id },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ async function AddOrgMember(req, res, next) {
|
|||
return res.status(404).send('No organization exists with that id.');
|
||||
}
|
||||
|
||||
await result.addOrgMember(user.userID);
|
||||
await result.addMember(user.id);
|
||||
|
||||
return res.send('User added to organization.');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ async function CreateOrg(req, res, next) {
|
|||
}
|
||||
|
||||
const existing = await user.countOwnedOrg({
|
||||
where: { orgName: orgName },
|
||||
where: { name: orgName },
|
||||
});
|
||||
if (existing > 0) {
|
||||
return res
|
||||
|
|
@ -16,7 +16,7 @@ async function CreateOrg(req, res, next) {
|
|||
}
|
||||
|
||||
const org = await user.createOwnedOrg({
|
||||
orgName: orgName,
|
||||
name: orgName,
|
||||
});
|
||||
|
||||
return res.status(200).send(org);
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ async function DeleteOrg(req, res, next) {
|
|||
}
|
||||
|
||||
await Organization.destroy({
|
||||
where: { orgID: orgID, ownerID: user.userID },
|
||||
where: { id: orgID, ownerID: user.id },
|
||||
});
|
||||
|
||||
return res.send('Organization deleted');
|
||||
|
|
|
|||
|
|
@ -8,13 +8,13 @@ async function GetOrg(req, res, next) {
|
|||
attributes: { exclude: ['updatedAt'] },
|
||||
include: [
|
||||
{
|
||||
association: 'orgOwner',
|
||||
association: 'owner',
|
||||
attributes: {
|
||||
exclude: ['userID', 'password', 'createdAt', 'updatedAt'],
|
||||
exclude: ['id', 'password', 'createdAt', 'updatedAt'],
|
||||
},
|
||||
},
|
||||
{
|
||||
association: 'orgMember',
|
||||
association: 'member',
|
||||
attributes: {
|
||||
exclude: ['password', 'createdAt', 'updatedAt'],
|
||||
},
|
||||
|
|
@ -23,11 +23,11 @@ async function GetOrg(req, res, next) {
|
|||
],
|
||||
});
|
||||
|
||||
const status = org.ownerID == user.userID;
|
||||
const status = org.ownerID == user.id;
|
||||
|
||||
return res.send({
|
||||
org,
|
||||
memberCount: org.orgMember.length,
|
||||
memberCount: org.member.length,
|
||||
status,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ async function RemoveOrgMember(req, res, next) {
|
|||
const doomedUserIDs = req.body.doomedUsers;
|
||||
|
||||
const result = await Organization.findByPk(orgID, {
|
||||
include: { association: 'orgOwner' },
|
||||
include: { association: 'owner' },
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
|
|
@ -14,10 +14,10 @@ async function RemoveOrgMember(req, res, next) {
|
|||
}
|
||||
|
||||
if (!doomedUserIDs) {
|
||||
await result.removeOrgMember(user.userID);
|
||||
await result.removeMember(user.id);
|
||||
} else {
|
||||
if (user.userID == result.orgOwner.userID) {
|
||||
await result.removeOrgMember(doomedUserIDs);
|
||||
if (user.id == result.owner.id) {
|
||||
await result.removeMember(doomedUserIDs);
|
||||
} else {
|
||||
return res
|
||||
.status(403)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ async function UpdateOrgDetails(req, res, next) {
|
|||
return res.status(400).send('Form missing required information.');
|
||||
}
|
||||
|
||||
const result = await user.getOwnedOrg({ where: { orgID: orgID } });
|
||||
const result = await user.getOwnedOrg({ where: { id: orgID } });
|
||||
|
||||
if (result.length < 1) {
|
||||
return res
|
||||
|
|
@ -15,7 +15,7 @@ async function UpdateOrgDetails(req, res, next) {
|
|||
.send('You do not own an organization with that id.');
|
||||
}
|
||||
|
||||
result[0].update({ orgName: orgName });
|
||||
result[0].update({ name: orgName });
|
||||
|
||||
return res.send(result[0]);
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue