New database with migrations

This commit is contained in:
Jori Lallo
2016-06-20 00:18:03 -07:00
parent f2732aacab
commit 24e02bfdc4
12 changed files with 361 additions and 49 deletions

View File

@@ -13,11 +13,11 @@ router.post('atlases.info', auth(), async (ctx) => {
let { id } = ctx.request.body;
ctx.assertPresent(id, 'id is required');
const team = await ctx.state.user.getTeam();
const user = ctx.state.user;
const atlas = await Atlas.findOne({
where: {
id: id,
teamId: team.id,
teamId: user.teamId,
},
});
@@ -30,10 +30,10 @@ router.post('atlases.info', auth(), async (ctx) => {
router.post('atlases.list', auth(), pagination(), async (ctx) => {
const team = await ctx.state.user.getTeam();
const user = ctx.state.user;
const atlases = await Atlas.findAll({
where: {
teamId: team.id,
teamId: user.teamId,
},
order: [
['updatedAt', 'DESC'],

View File

@@ -40,13 +40,27 @@ router.post('auth.slack', async (ctx) => {
const authResponse = await fetch(`https://slack.com/api/auth.test?token=${data.access_token}`);
const authData = await authResponse.json();
// Team
let team = await Team.findOne({ where: { slackId: data.team.id } });
if (!team) {
team = await Team.create({
name: data.team.name,
slackId: data.team.id,
slackData: data.team,
});
const atlas = await team.createFirstAtlas();
} else {
team.name = data.team.name;
team.slackData = data.team;
team = await team.save();
}
if (user) {
user.slackAccessToken = data.access_token;
user.slackData = data.user;
user = await user.save();
} else {
// Existing user
user = await User.create({
user = await team.createUser({
slackId: data.user.id,
username: authData.user,
name: data.user.name,
@@ -56,30 +70,11 @@ router.post('auth.slack', async (ctx) => {
});
}
// Team
let team = await Team.findOne({ where: { slackId: data.team.id } });
if (!team) {
team = await Team.create({
name: data.team.name,
slackId: data.team.id,
slackData: data.team,
});
} else {
// Update data
team.name = data.team.name;
team.slackData = data.team;
team = await team.save();
}
// Add to correct team
user.setTeam(team);
ctx.body = { data: {
user: await presentUser(user),
team: await presentTeam(team),
accessToken: user.getJwtToken(),
}};
console.log("enf")
});
export default router;

View File

@@ -23,8 +23,8 @@ router.post('documents.info', auth({ require: false }), async (ctx) => {
if (document.private) {
if (!ctx.state.user) throw httpErrors.NotFound();
const team = await ctx.state.user.getTeam();
if (document.teamId !== team.id) {
const user = await ctx.state.user;
if (document.teamId !== user.teamId) {
throw httpErrors.NotFound();
}
@@ -52,11 +52,10 @@ router.post('documents.create', auth(), async (ctx) => {
ctx.assertPresent(text, 'text is required');
const user = ctx.state.user;
const team = await user.getTeam();
const ownerAtlas = await Atlas.findOne({
where: {
id: atlas,
teamId: team.id,
teamId: user.teamId,
},
});
@@ -64,7 +63,7 @@ router.post('documents.create', auth(), async (ctx) => {
const document = await Document.create({
atlasId: ownerAtlas.id,
teamId: team.id,
teamId: user.teamId,
userId: user.id,
title: title,
text: text,
@@ -86,11 +85,10 @@ router.post('documents.update', auth(), async (ctx) => {
ctx.assertPresent(text, 'text is required');
const user = ctx.state.user;
const team = await user.getTeam();
let document = await Document.findOne({
where: {
id: id,
teamId: team.id,
teamId: user.teamId,
},
});
@@ -112,11 +110,10 @@ router.post('documents.delete', auth(), async (ctx) => {
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const team = await user.getTeam();
let document = await Document.findOne({
where: {
id: id,
teamId: team.id,
teamId: user.teamId,
},
});