Skip to content

Commit

Permalink
remaining task: documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehss committed Jun 24, 2021
1 parent 5957bad commit e380744
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ DEV_PASSWORD=
DEV_SERVER=localhost
DEV_DB=kanban_db

DEV_G_CLIENT_ID=982085477024-6n2tpq82aracq5krun8e705oj863h0q2.apps.googleusercontent.com
DEV_G_CLIENT_ID=982085477024-5msqv561q4b1jj1je93dvrs5dcseu1b2.apps.googleusercontent.com
16 changes: 13 additions & 3 deletions controllers/taskController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const {Task} = require('../models/index')
const {Task, User} = require('../models/index')
const {jwtEncrypt, jwtDecrypt} = require('../helpers/jwt')
const {compareHash} = require('../helpers/brcypt')

class Controller{
static postTask(req, res, next){
console.log('posting task')
let task = req.body
console.log(task)
console.log(req.currentUser)
Expand All @@ -17,8 +18,16 @@ class Controller{
}

static getTask(req, res, next){
Task.findAll()
Task.findAll({include:[{
model: User,
attributes: ["alias", "id"]
}]
})
.then(taskData => {
// console.log(taskData)
taskData.forEach(el => {
if(el.User.id == req.currentUser.id) {el.dataValues.editable = true}
})
res.status(200).json(taskData)
})
.catch(err => {next(err)})
Expand All @@ -33,8 +42,9 @@ class Controller{
}

static putTask(req, res, next){
console.log('putting task')
let data = req.body
data.deadline = new Date()
console.log(req.body)
Task.update(data, {where: {id:req.params.id}, returning:true})
.then(results =>{
res.status(200).json(results[1])
Expand Down
12 changes: 7 additions & 5 deletions controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { User} = require('../models/index')
const {jwtEncrypt, jwtDecrypt} = require('../helpers/jwt')
const {compareHash} = require('../helpers/brcypt')
const {OAuth2Client} = require('google-auth-library');
const CLIENT_ID = 'temp'
const client = new OAuth2Client(CLIENT_ID);
const {OAuth2Client} = require('google-auth-library')
const GCLIENT_ID = process.env.GCLIENT_ID
const client = new OAuth2Client(GCLIENT_ID);


class Controller{
static postRegister(req, res, next){
console.log('test')
if (!req.body.email || !req.body.password) throw {name: "FillEmailPassword"}
User.create(req.body)
.then(() => {
Expand Down Expand Up @@ -35,14 +36,15 @@ class Controller{
}

static gLogin(req, res, next){
let gmail = ""
client.verifyIdToken({
idToken: req.body.idToken,
audience: GCLIENT_ID,
})
.then(ticket =>{
const payload = ticket.getPayload();
const gmail = payload['email'];
return User.findOne({where:{email:gmail}})
gmail = payload['email'];
return User.findOne({where:{email: gmail}})
})
.then(user => {
if (!user){
Expand Down
4 changes: 4 additions & 0 deletions middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const {User, Task} = require("../models")
const authentication = (req, res, next) =>{
try{
const {access_token} = req.headers
console.log(req.header)
console.log(access_token)
const dataDecoded = jwtDecrypt(access_token)
User.findByPk(dataDecoded.id)
.then(user => {
Expand All @@ -24,8 +26,10 @@ const authentication = (req, res, next) =>{

const authorization = (req, res, next) => {
let id = req.params.id
console.log('authorization')
Task.findOne({where:{id:id}})
.then(task =>{
console.log('auth')
if (!task) {
throw {
name: "TaskNotFound",
Expand Down
3 changes: 3 additions & 0 deletions migrations/20210611131608-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module.exports = {
type: Sequelize.STRING,
allowNull: false
},
alias: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
Expand Down
7 changes: 2 additions & 5 deletions migrations/20210611133056-create-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ module.exports = {
allowNull: false,
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
deadline: {
type: Sequelize.DATE
},
category: {
allowNull: false,
type: Sequelize.STRING
type: Sequelize.STRING,
allowNull: false
},
UserId: {
type: Sequelize.INTEGER,
Expand Down
18 changes: 1 addition & 17 deletions models/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,19 @@ module.exports = (sequelize, DataTypes) => {
notEmpty: {msg: "Task name may not be empty"}
}
},
description: DataTypes.STRING,
deadline: {
type: DataTypes.DATE,
validate:{
dateCheck(taskDate) {
if (taskDate) {
let taskDate = taskDate.getTime()
let date = new Date()
date = new Date(date.toISOString().slice(0,10).getTime())
if (taskDate < date) throw {msg: "Deadline must be after today"}
}
}
}
},
category: {
type: DataTypes.STRING,
validate: {
categories(cat){
if (cat != "backlog" && cat != "todo" && cat != "doing" && cat != "done") throw {msg: "Invalid Category"}
}
}
},
UserId: DataTypes.INTEGER
}, {
hooks:{
beforeValidate: task => {
if (!task.deadline) {task.deadline = null}
if (!task.deadline) {task.deadline = null}

console.log(task.deadline)
}
},
sequelize,
Expand Down
4 changes: 4 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ module.exports = (sequelize, DataTypes) => {
}
}
},
alias: {
type: DataTypes.STRING
}
}, {
hooks:{
beforeCreate: user =>{
user.password = hash(user.password)
user.email = user.email.toLowerCase()
if (!user.alias) {user.alias = user.email}
}
},
sequelize,
Expand Down
Loading

0 comments on commit e380744

Please sign in to comment.