Skip to content

Commit

Permalink
add delete customer logic; b00tc4mp#167
Browse files Browse the repository at this point in the history
  • Loading branch information
j0sep0z0 committed Aug 13, 2024
1 parent ea700dc commit 6293683
Show file tree
Hide file tree
Showing 18 changed files with 207 additions and 13 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
48 changes: 48 additions & 0 deletions h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
commit 0fa059608bb93212c918d7681f55c2a7b9f64964 (HEAD -> feature/project)
Author: j0sep0z0 <[email protected]>
Date: Tue Aug 6 00:20:38 2024 +0200

add customer, service, appointment and note models, change user model and some logics; #167

commit 1abf5c8555b7e779985ae6bfa3896e9304df997c (origin/feature/project)
Author: j0sep0z0 <[email protected]>
Date: Fri Aug 2 19:32:02 2024 +0200

add register, login, isuserloggedin app logics and render routes; #167

commit e61a03e7b5aaa8b582311e4d2ce94061a589f878
Author: j0sep0z0 <[email protected]>
Date: Wed Jul 31 22:33:35 2024 +0200

create base package for api and app, add com, add register and authenticate api logics; #167

commit 808cff2588bc90818573b2092824bcbcdbd95e10
Merge: cd49caa a7a73f6
Author: j0sep0z0 <[email protected]>
Date: Sat Jun 22 00:55:54 2024 +0200

Merge branch 'feature/project' of https://github.com/j0sep0z0/isdi-parttime-202403 into feature/project

commit cd49caa13a22b8f67c6641cf71a0a31112bf4c08
Author: j0sep0z0 <[email protected]>
Date: Sat Jun 22 00:29:47 2024 +0200

add branch project with readme file; #167

commit a7a73f6cb612dda353634e5208fdfde3fc556d83
Author: j0sep0z0 <[email protected]>
Date: Sun May 19 07:22:42 2024 +0200

add final project; #167

commit fb2004e8c4c72649e852edd6df2d83ed2c767519 (origin/develop, develop)
Author: j0sep0z0 <[email protected]>
Date: Wed Mar 20 21:35:45 2024 +0100

add my folder with git ignore rules #8

commit 7a107ffe4be750d761c298d5e2a4de80fd9be162 (upstream/main, origin/main, origin/HEAD, main)
Author: manuelbarzi <[email protected]>
Date: Tue Mar 19 19:18:09 2024 +0100

Initial commit
1 change: 1 addition & 0 deletions staff/jose-pozo/project/api/data/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const user = new Schema({

password: {
type: String,

},

role: {
Expand Down
33 changes: 33 additions & 0 deletions staff/jose-pozo/project/api/handlers/deleteCustomerHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import jwt from '../util/jsonwebtoken-promised.js'

import logic from '../logic/index.js'

import { CredentialsError, SystemError } from 'com/errors.js'

const { JWT_SECRET } = process.env

export default (req, res, next) => {

try {
const token = req.headers.authorization.slice(7)

jwt.verify(token, JWT_SECRET)
.then(payload => {
const { sub: userId } = payload

const { targetUserId } = req.params

try {
logic.deleteCustomer(userId, targetUserId)
.then(user => res.json(user))
.catch(error => next(error))
} catch (error) {
next(error)
}
})
.catch(error => next(new CredentialsError(error.message)))
} catch (error) {
next(error)
}
}

4 changes: 4 additions & 0 deletions staff/jose-pozo/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import getUserProfileHandler from './getUserProfileHandler.js'

import createCustomerHandler from './createCustomerHandler.js'

import deleteCustomerHandler from './deleteCustomerHandler.js'


export default {
errorHandler,
Expand All @@ -21,4 +23,6 @@ export default {
getUserProfileHandler,

createCustomerHandler,

deleteCustomerHandler,
}
36 changes: 36 additions & 0 deletions staff/jose-pozo/project/api/logic/deleteCustomer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { User } from '../data/index.js'
import { NotFoundError, SystemError } from 'com/errors.js'
import validate from 'com/validate.js'


const deleteCustomer = (userId, targetUserId) => {

validate.id(userId, 'userId')
validate.id(targetUserId, 'targetId')

return User.findById(userId).select('-__v').lean()
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (!user) throw new NotFoundError('User not found')

return User.updateOne({ _id: userId }, { $pull: { customers: targetUserId } })
.catch(error => { throw new SystemError(error.message) })
.then(() => {

return User.findById(targetUserId).select('-__v').lean()
.catch(error => { throw new SystemError(error.message) })
.then(targetUser => {
if (!targetUser) throw new NotFoundError('targetUser not found')

return User.deleteOne({ _id: targetUserId })
.catch(error => { throw new SystemError(error.message) })
.then(() => {

return targetUserId._id
})
})
})
})
}

export default deleteCustomer
2 changes: 2 additions & 0 deletions staff/jose-pozo/project/api/logic/deleteCustomer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'dotenv'
import mongoose, { }
20 changes: 20 additions & 0 deletions staff/jose-pozo/project/api/logic/deleteCustomer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// debugger

import 'dotenv/config'
import mongoose from 'mongoose'

import deleteCustomer from './deleteCustomer.js'

const { MONGODB_URL } = process.env

mongoose.connect(MONGODB_URL)
.then(() => {
try {
deleteCustomer('66bbbccf0eacbc11ec4c6289', '66bbbd380eacbc11ec4c628f')
.then((user) => console.log('user deleted'))
.catch(error => console.error(error))
} catch (error) {
console.error(error)
}
})
.catch(error => console.error(error))
4 changes: 4 additions & 0 deletions staff/jose-pozo/project/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import getUserProfile from './getUserProfile.js'

import createCustomer from './createCustomer.js'

import deleteCustomer from './deleteCustomer.js'


const logic = {
registerUser,
Expand All @@ -17,6 +19,8 @@ const logic = {
getUserProfile,

createCustomer,

deleteCustomer,
}


Expand Down
5 changes: 3 additions & 2 deletions staff/jose-pozo/project/api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ router.post('/users/auth', jsonBodyParser, routes.authenticateUserHandler)

router.post('/users/customers', jsonBodyParser, routes.createCustomerHandler)

router.delete('/users/:targetUserId', routes.deleteCustomerHandler)

router.get('/users/:targetUserId', routes.getUserNameHandler)
router.get('/users', routes.getAllCustomersHandler)
router.get('/users/:targetUserId/profile', routes.getUserProfileHandler)

router.get('/users', routes.getAllCustomersHandler)


export default router
Expand Down
2 changes: 1 addition & 1 deletion staff/jose-pozo/project/api/test/authenticate-user.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"email": "daenerys@targaryen.com",
"email": "jon@snow.com",
"password": "1234"
}' \
-v \
Expand Down
13 changes: 9 additions & 4 deletions staff/jose-pozo/project/api/test/create-customer.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
curl -X POST \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmIzYTZkNzQ3ODQ2MmQxMjAyODA2NmYiLCJpYXQiOjE3MjMxMDAyNzksImV4cCI6MTcyMzEyMTg3OX0.reN7eA0ZCZ5Hu8il54o6UE9242CaC-Uun1hQ32wZ9oc" \
-H "Content-Type: application/json" \
-v \
http://localhost:2011/users/customers
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmJiYmNjZjBlYWNiYzExZWM0YzYyODkiLCJpYXQiOjE3MjM1ODM2MjYsImV4cCI6MTcyMzYwNTIyNn0.8AWhOrlOXqKAJfdYReYrlgdwS5M32caDYLNaHMXIj4g" \
-H "Content-Type: application/json" \
-d '{
"name": "Alba",
"surname": "Rinho",
"email": "[email protected]"
}' \
-v \
http://localhost:2011/users/customers
6 changes: 6 additions & 0 deletions staff/jose-pozo/project/api/test/delete-customer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
curl -X DELETE \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmJiYmNjZjBlYWNiYzExZWM0YzYyODkiLCJpYXQiOjE3MjM1ODM2MjYsImV4cCI6MTcyMzYwNTIyNn0.8AWhOrlOXqKAJfdYReYrlgdwS5M32caDYLNaHMXIj4g" \
-H "Content-Type: application/json" \
-v \
http://localhost:2011/users/66bbce27013ef813c9c22ccb

5 changes: 3 additions & 2 deletions staff/jose-pozo/project/api/test/get-all-customers.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmIzYTZkNzQ3ODQ2MmQxMjAyODA2NmYiLCJpYXQiOjE3MjMxODk3NzksImV4cCI6MTcyMzIxMTM3OX0.tndoxdw0ZLGtsq7U5u6CX5yn45SXpZcNidynhp5EUsk" \
-H "Content-Type: application/json" \
http://localhost:2011/users \
-v
-v \
http://localhost:2011/users

4 changes: 2 additions & 2 deletions staff/jose-pozo/project/api/test/get-user-name.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmIyNDY1YWY4NTcwNTE1YjQ1MjFlZTciLCJpYXQiOjE3MjI5NTk3NzIsImV4cCI6MTcyMjk4MTM3Mn0.wIhibxwOh5i9NhIhvGE0lfB-Cn0R7VQIGRVjIi7pJJ0" \
-v \
http://localhost:2011/users/66b24206b23e045e192c700b
-v \
http://localhost:2011/users/66b24206b23e045e192c700b
4 changes: 3 additions & 1 deletion staff/jose-pozo/project/api/test/get-user-profile.sh
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
curl http://localhost:2011/users/66b4abe16a6a9df84cfda6f9/profile/ -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmIzYTZkNzQ3ODQ2MmQxMjAyODA2NmYiLCJpYXQiOjE3MjMzODU5NzMsImV4cCI6MTcyMzQwNzU3M30.YMPnnz6-RLpArcp8Cpj-nS2PrzRzhufLKwORs5Le1mQ" -v
curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmIzYTZkNzQ3ODQ2MmQxMjAyODA2NmYiLCJpYXQiOjE3MjMzODU5NzMsImV4cCI6MTcyMzQwNzU3M30.YMPnnz6-RLpArcp8Cpj-nS2PrzRzhufLKwORs5Le1mQ" \
-v \
http://localhost:2011/users/66b4abe16a6a9df84cfda6f9/profile/
31 changes: 31 additions & 0 deletions staff/jose-pozo/project/app/src/logic/deleteCustomer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import errors, { SystemError } from 'com/errors'


const deleteCustomer = (targetUserId) => {

return fetch(`${import.meta.env.VITE_API_URL}/users/${targetUserId}`, {
method: 'DELETE',

headers: {
Authorization: `Bearer ${sessionStorage.token}`
}
})

.catch(() => { throw new SystemError('server error') })
.then(response => {
if (response.status === 200)
return response.json()
.catch(() => { throw new SystemError('server error') })
.then(targetUserId => targetUserId)

return response.json()
.catch(() => { throw new SystemError('server error') })
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
1 change: 0 additions & 1 deletion staff/jose-pozo/project/app/src/logic/getAllCustomers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import errors, { SystemError } from 'com/errors'
import validate from 'com/validate'


const getAllCustomers = () => {
Expand Down

0 comments on commit 6293683

Please sign in to comment.