Skip to content

Commit

Permalink
fix: profile routes return
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusasferreira committed Dec 30, 2021
1 parent 8acee73 commit aae4d27
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
5 changes: 3 additions & 2 deletions __tests__/unit/profileService.test.ts
Expand Up @@ -42,13 +42,14 @@ describe('Profile Service', () => {
name: 'name',
bio: 'bio',
birthDate: 'birthdate',
location: 'location'
location: 'location',
user: {id: '1'}
}

await ProfileService.update(options)

expect(mockedS3Service.uploadFile).toBeCalledTimes(1)
expect(mockedS3Service.uploadFile).toBeCalledWith(options.file);
expect(mockedS3Service.uploadFile).toBeCalledWith("file");
expect(mockedTypeorm.getRepository(Profile).update).toBeCalledTimes(1)
})
})
11 changes: 5 additions & 6 deletions src/controllers/ProfileController.ts
@@ -1,6 +1,5 @@
import ProfileService from '@services/ProfileService'
import { Request, Response } from 'express'
import fs from 'fs'
import { validationResult } from 'express-validator'
import S3Service from '@services/external/s3'

Expand All @@ -17,7 +16,7 @@ class ProfileController {
})
}

await ProfileService.create({
const profile = await ProfileService.create({
file: req.file,
name: req.body.name,
bio: req.body.bio,
Expand All @@ -26,7 +25,7 @@ class ProfileController {
user: req.body.user
})

res.status(200).json({message: 'profile created sucessfully'})
res.status(200).json(profile)
} catch (e) {
console.log(e)
res.status(400).json({errors: [{ message: e.message}] })
Expand All @@ -49,7 +48,7 @@ class ProfileController {

const profile = await ProfileService.get(id)

res.status(200).json({profile})
res.status(200).json(profile)
} catch (e) {
console.log(e)
res.status(400).json({errors: [{message: e.message}]})
Expand All @@ -68,7 +67,7 @@ class ProfileController {
})
}

await ProfileService.update({
const profile = await ProfileService.update({
file: req.file,
name: req.body.name,
bio: req.body.bio,
Expand All @@ -77,7 +76,7 @@ class ProfileController {
user: req.body.user
})

res.status(200).json({message: 'successfully updated profile'})
res.status(200).json(profile)
} catch (e) {
console.log(e)
res.status(400).json({errors: [{message: e.message}]})
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/ensureAuthenticated.ts
Expand Up @@ -10,7 +10,7 @@ export default function ensureAuthenticated(req: Request, res: Response, next: N

const decoded = jwt.verify(token, process.env.JWT_SECRET) as any

req.body.user = decoded.payload.id
req.body.user = decoded.payload

next()
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/routes.ts
Expand Up @@ -23,7 +23,7 @@ routes.post('/users', validate('create-user'), UserController.create)
routes.post('/login', validate('login'), UserController.login)
routes.delete('/logout', validate('logout'), ensureAuthenticated, UserController.logout)
routes.patch('/recover-password', validate('recover-password'), UserController.recoverPassword)
routes.post('/change-password', validate('recover-password'), ensureAuthenticated, UserController.changePassword)
routes.post('/change-password', validate('change-password'), ensureAuthenticated, UserController.changePassword)
routes.delete('/users', ensureAuthenticated, UserController.delete)
routes.get('/confirmation/:token', validate('confirm-email'), EmailConfirmationController.confirm)
routes.post('/token', validate('refresh-token'), UserController.refreshToken)
Expand Down
37 changes: 25 additions & 12 deletions src/services/ProfileService.ts
Expand Up @@ -3,13 +3,13 @@ import S3Service from '@services/external/s3'
import {getRepository} from 'typeorm'

class ProfileService {
async create(options){
async create(options): Promise<Profile>{
const profileRepo = getRepository(Profile)

const resultFile = await S3Service.uploadFile(options.file)

const newProfile = await profileRepo.create({
id: options.user,
user: options.user,
name: options.name,
bio: options.bio,
picture: resultFile,
Expand All @@ -18,32 +18,45 @@ class ProfileService {
})

await profileRepo.save(newProfile)

return newProfile
}

async get(id){
async get(id): Promise<Profile>{
const profileRepo = getRepository(Profile)

let profile = await profileRepo.findOne(id)

if(!profile) throw new Error('Profile doesn\'t exists')


const parsedPicture = `/images/${profile.picture}`

profile = {...profile, picture: parsedPicture}

return profile
}

async update(options){
async update(options): Promise<Profile>{
const profileRepo = getRepository(Profile)

const fk = options.user.id

delete options.user

if(options.file){
const resultFile = await S3Service.uploadFile(options.file)

Object.assign(options, {picture: resultFile})
}

const resultFile = await S3Service.uploadFile(options.file)
delete options.file

await profileRepo.update(options.user, {
name: options.name,
bio: options.bio,
picture: resultFile,
birth_date: options.birth_date,
location: options.location,
})
await profileRepo.update({userId: fk}, options)

const updatedProfile = profileRepo.findOne({userId: fk})

return updatedProfile
}
}

Expand Down

0 comments on commit aae4d27

Please sign in to comment.