From 6293683fa40d95ad4d8b32d1a0408cda69330831 Mon Sep 17 00:00:00 2001 From: j0sep0z0 Date: Tue, 13 Aug 2024 23:58:55 +0200 Subject: [PATCH] add delete customer logic; #167 --- .vscode/settings.json | 1 + h | 48 +++++++++++++++++++ staff/jose-pozo/project/api/data/User.js | 1 + .../api/handlers/deleteCustomerHandler.js | 33 +++++++++++++ staff/jose-pozo/project/api/handlers/index.js | 4 ++ .../project/api/logic/deleteCustomer.js | 36 ++++++++++++++ .../project/api/logic/deleteCustomer.spec.js | 2 + .../project/api/logic/deleteCustomer.test.js | 20 ++++++++ staff/jose-pozo/project/api/logic/index.js | 4 ++ staff/jose-pozo/project/api/routes.js | 5 +- .../project/api/test/authenticate-user.sh | 2 +- .../project/api/test/create-customer.sh | 13 +++-- .../project/api/test/delete-customer.sh | 6 +++ .../project/api/test/get-all-customers.sh | 5 +- .../project/api/test/get-user-name.sh | 4 +- .../project/api/test/get-user-profile.sh | 4 +- .../project/app/src/logic/deleteCustomer.js | 31 ++++++++++++ .../project/app/src/logic/getAllCustomers.js | 1 - 18 files changed, 207 insertions(+), 13 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 h create mode 100644 staff/jose-pozo/project/api/handlers/deleteCustomerHandler.js create mode 100644 staff/jose-pozo/project/api/logic/deleteCustomer.js create mode 100644 staff/jose-pozo/project/api/logic/deleteCustomer.spec.js create mode 100644 staff/jose-pozo/project/api/logic/deleteCustomer.test.js create mode 100644 staff/jose-pozo/project/api/test/delete-customer.sh create mode 100644 staff/jose-pozo/project/app/src/logic/deleteCustomer.js diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/h b/h new file mode 100644 index 000000000..a195f1fd6 --- /dev/null +++ b/h @@ -0,0 +1,48 @@ +commit 0fa059608bb93212c918d7681f55c2a7b9f64964 (HEAD -> feature/project) +Author: j0sep0z0 +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 +Date: Fri Aug 2 19:32:02 2024 +0200 + + add register, login, isuserloggedin app logics and render routes; #167 + +commit e61a03e7b5aaa8b582311e4d2ce94061a589f878 +Author: j0sep0z0 +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 +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 +Date: Sat Jun 22 00:29:47 2024 +0200 + + add branch project with readme file; #167 + +commit a7a73f6cb612dda353634e5208fdfde3fc556d83 +Author: j0sep0z0 +Date: Sun May 19 07:22:42 2024 +0200 + + add final project; #167 + +commit fb2004e8c4c72649e852edd6df2d83ed2c767519 (origin/develop, develop) +Author: j0sep0z0 +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 +Date: Tue Mar 19 19:18:09 2024 +0100 + + Initial commit diff --git a/staff/jose-pozo/project/api/data/User.js b/staff/jose-pozo/project/api/data/User.js index be67c127d..f31ce50e0 100644 --- a/staff/jose-pozo/project/api/data/User.js +++ b/staff/jose-pozo/project/api/data/User.js @@ -19,6 +19,7 @@ const user = new Schema({ password: { type: String, + }, role: { diff --git a/staff/jose-pozo/project/api/handlers/deleteCustomerHandler.js b/staff/jose-pozo/project/api/handlers/deleteCustomerHandler.js new file mode 100644 index 000000000..4d7320414 --- /dev/null +++ b/staff/jose-pozo/project/api/handlers/deleteCustomerHandler.js @@ -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) + } +} + diff --git a/staff/jose-pozo/project/api/handlers/index.js b/staff/jose-pozo/project/api/handlers/index.js index 4a5b416c8..f96fb1f82 100644 --- a/staff/jose-pozo/project/api/handlers/index.js +++ b/staff/jose-pozo/project/api/handlers/index.js @@ -9,6 +9,8 @@ import getUserProfileHandler from './getUserProfileHandler.js' import createCustomerHandler from './createCustomerHandler.js' +import deleteCustomerHandler from './deleteCustomerHandler.js' + export default { errorHandler, @@ -21,4 +23,6 @@ export default { getUserProfileHandler, createCustomerHandler, + + deleteCustomerHandler, } \ No newline at end of file diff --git a/staff/jose-pozo/project/api/logic/deleteCustomer.js b/staff/jose-pozo/project/api/logic/deleteCustomer.js new file mode 100644 index 000000000..4531461c4 --- /dev/null +++ b/staff/jose-pozo/project/api/logic/deleteCustomer.js @@ -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 \ No newline at end of file diff --git a/staff/jose-pozo/project/api/logic/deleteCustomer.spec.js b/staff/jose-pozo/project/api/logic/deleteCustomer.spec.js new file mode 100644 index 000000000..fc47547f6 --- /dev/null +++ b/staff/jose-pozo/project/api/logic/deleteCustomer.spec.js @@ -0,0 +1,2 @@ +import 'dotenv' +import mongoose, { } \ No newline at end of file diff --git a/staff/jose-pozo/project/api/logic/deleteCustomer.test.js b/staff/jose-pozo/project/api/logic/deleteCustomer.test.js new file mode 100644 index 000000000..11d736f14 --- /dev/null +++ b/staff/jose-pozo/project/api/logic/deleteCustomer.test.js @@ -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)) diff --git a/staff/jose-pozo/project/api/logic/index.js b/staff/jose-pozo/project/api/logic/index.js index 8290687da..dd590b385 100644 --- a/staff/jose-pozo/project/api/logic/index.js +++ b/staff/jose-pozo/project/api/logic/index.js @@ -7,6 +7,8 @@ import getUserProfile from './getUserProfile.js' import createCustomer from './createCustomer.js' +import deleteCustomer from './deleteCustomer.js' + const logic = { registerUser, @@ -17,6 +19,8 @@ const logic = { getUserProfile, createCustomer, + + deleteCustomer, } diff --git a/staff/jose-pozo/project/api/routes.js b/staff/jose-pozo/project/api/routes.js index f5c582ec3..4d97a7d1e 100644 --- a/staff/jose-pozo/project/api/routes.js +++ b/staff/jose-pozo/project/api/routes.js @@ -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 diff --git a/staff/jose-pozo/project/api/test/authenticate-user.sh b/staff/jose-pozo/project/api/test/authenticate-user.sh index b46abf015..7c3eedb7e 100644 --- a/staff/jose-pozo/project/api/test/authenticate-user.sh +++ b/staff/jose-pozo/project/api/test/authenticate-user.sh @@ -1,7 +1,7 @@ curl -X POST \ -H "Content-Type: application/json" \ -d '{ - "email": "daenerys@targaryen.com", + "email": "jon@snow.com", "password": "1234" }' \ -v \ diff --git a/staff/jose-pozo/project/api/test/create-customer.sh b/staff/jose-pozo/project/api/test/create-customer.sh index e1d983339..36ccc3706 100644 --- a/staff/jose-pozo/project/api/test/create-customer.sh +++ b/staff/jose-pozo/project/api/test/create-customer.sh @@ -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": "alba@rinho.com" +}' \ +-v \ +http://localhost:2011/users/customers diff --git a/staff/jose-pozo/project/api/test/delete-customer.sh b/staff/jose-pozo/project/api/test/delete-customer.sh new file mode 100644 index 000000000..93e3e65a8 --- /dev/null +++ b/staff/jose-pozo/project/api/test/delete-customer.sh @@ -0,0 +1,6 @@ +curl -X DELETE \ +-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmJiYmNjZjBlYWNiYzExZWM0YzYyODkiLCJpYXQiOjE3MjM1ODM2MjYsImV4cCI6MTcyMzYwNTIyNn0.8AWhOrlOXqKAJfdYReYrlgdwS5M32caDYLNaHMXIj4g" \ +-H "Content-Type: application/json" \ +-v \ +http://localhost:2011/users/66bbce27013ef813c9c22ccb + diff --git a/staff/jose-pozo/project/api/test/get-all-customers.sh b/staff/jose-pozo/project/api/test/get-all-customers.sh index 5ddcb1296..16991dc45 100644 --- a/staff/jose-pozo/project/api/test/get-all-customers.sh +++ b/staff/jose-pozo/project/api/test/get-all-customers.sh @@ -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 + diff --git a/staff/jose-pozo/project/api/test/get-user-name.sh b/staff/jose-pozo/project/api/test/get-user-name.sh index f05a5c306..5e80166d4 100644 --- a/staff/jose-pozo/project/api/test/get-user-name.sh +++ b/staff/jose-pozo/project/api/test/get-user-name.sh @@ -1,3 +1,3 @@ curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmIyNDY1YWY4NTcwNTE1YjQ1MjFlZTciLCJpYXQiOjE3MjI5NTk3NzIsImV4cCI6MTcyMjk4MTM3Mn0.wIhibxwOh5i9NhIhvGE0lfB-Cn0R7VQIGRVjIi7pJJ0" \ - -v \ - http://localhost:2011/users/66b24206b23e045e192c700b +-v \ +http://localhost:2011/users/66b24206b23e045e192c700b diff --git a/staff/jose-pozo/project/api/test/get-user-profile.sh b/staff/jose-pozo/project/api/test/get-user-profile.sh index f13b0a003..b8adcf0ab 100644 --- a/staff/jose-pozo/project/api/test/get-user-profile.sh +++ b/staff/jose-pozo/project/api/test/get-user-profile.sh @@ -1 +1,3 @@ -curl http://localhost:2011/users/66b4abe16a6a9df84cfda6f9/profile/ -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmIzYTZkNzQ3ODQ2MmQxMjAyODA2NmYiLCJpYXQiOjE3MjMzODU5NzMsImV4cCI6MTcyMzQwNzU3M30.YMPnnz6-RLpArcp8Cpj-nS2PrzRzhufLKwORs5Le1mQ" -v \ No newline at end of file +curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmIzYTZkNzQ3ODQ2MmQxMjAyODA2NmYiLCJpYXQiOjE3MjMzODU5NzMsImV4cCI6MTcyMzQwNzU3M30.YMPnnz6-RLpArcp8Cpj-nS2PrzRzhufLKwORs5Le1mQ" \ +-v \ +http://localhost:2011/users/66b4abe16a6a9df84cfda6f9/profile/ diff --git a/staff/jose-pozo/project/app/src/logic/deleteCustomer.js b/staff/jose-pozo/project/app/src/logic/deleteCustomer.js new file mode 100644 index 000000000..d03cee512 --- /dev/null +++ b/staff/jose-pozo/project/app/src/logic/deleteCustomer.js @@ -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) + }) + }) +} \ No newline at end of file diff --git a/staff/jose-pozo/project/app/src/logic/getAllCustomers.js b/staff/jose-pozo/project/app/src/logic/getAllCustomers.js index 672f867c9..35aff77f5 100644 --- a/staff/jose-pozo/project/app/src/logic/getAllCustomers.js +++ b/staff/jose-pozo/project/app/src/logic/getAllCustomers.js @@ -1,5 +1,4 @@ import errors, { SystemError } from 'com/errors' -import validate from 'com/validate' const getAllCustomers = () => {