forked from b00tc4mp/isdi-parttime-202403
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add delete customer logic; b00tc4mp#167
- Loading branch information
Showing
18 changed files
with
207 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[33mcommit 0fa059608bb93212c918d7681f55c2a7b9f64964[m[33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature/project[m[33m)[m | ||
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 | ||
|
||
[33mcommit 1abf5c8555b7e779985ae6bfa3896e9304df997c[m[33m ([m[1;31morigin/feature/project[m[33m)[m | ||
Author: j0sep0z0 <[email protected]> | ||
Date: Fri Aug 2 19:32:02 2024 +0200 | ||
|
||
add register, login, isuserloggedin app logics and render routes; #167 | ||
|
||
[33mcommit e61a03e7b5aaa8b582311e4d2ce94061a589f878[m | ||
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 | ||
|
||
[33mcommit 808cff2588bc90818573b2092824bcbcdbd95e10[m | ||
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 | ||
|
||
[33mcommit cd49caa13a22b8f67c6641cf71a0a31112bf4c08[m | ||
Author: j0sep0z0 <[email protected]> | ||
Date: Sat Jun 22 00:29:47 2024 +0200 | ||
|
||
add branch project with readme file; #167 | ||
|
||
[33mcommit a7a73f6cb612dda353634e5208fdfde3fc556d83[m | ||
Author: j0sep0z0 <[email protected]> | ||
Date: Sun May 19 07:22:42 2024 +0200 | ||
|
||
add final project; #167 | ||
|
||
[33mcommit fb2004e8c4c72649e852edd6df2d83ed2c767519[m[33m ([m[1;31morigin/develop[m[33m, [m[1;32mdevelop[m[33m)[m | ||
Author: j0sep0z0 <[email protected]> | ||
Date: Wed Mar 20 21:35:45 2024 +0100 | ||
|
||
add my folder with git ignore rules #8 | ||
|
||
[33mcommit 7a107ffe4be750d761c298d5e2a4de80fd9be162[m[33m ([m[1;31mupstream/main[m[33m, [m[1;31morigin/main[m[33m, [m[1;31morigin/HEAD[m[33m, [m[1;32mmain[m[33m)[m | ||
Author: manuelbarzi <[email protected]> | ||
Date: Tue Mar 19 19:18:09 2024 +0100 | ||
|
||
Initial commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ const user = new Schema({ | |
|
||
password: { | ||
type: String, | ||
|
||
}, | ||
|
||
role: { | ||
|
33 changes: 33 additions & 0 deletions
33
staff/jose-pozo/project/api/handlers/deleteCustomerHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import 'dotenv' | ||
import mongoose, { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters