forked from b00tc4mp/eurofirms-bootcamp-202402
-
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 all cinema logics and test b00tc4mp#257
- Loading branch information
Showing
6 changed files
with
102 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Cinema, User } from '../data/index.js' | ||
|
||
import { validate, errors } from 'com' | ||
|
||
const { SystemError, MatchError } = errors | ||
|
||
function deleteCinemaToManager(userId, cinemaId) { | ||
validate.id(userId) | ||
validate.id(cinemaId, 'cinemaId') | ||
|
||
return User.findById(userId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) throw new MatchError('User not found') | ||
|
||
if (!user.cinema) throw new MatchError('You don´t have any cinema to delete') | ||
|
||
return Cinema.findById(cinemaId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(cinema => { | ||
if (!cinema) throw new MatchError('Cinema not found') | ||
|
||
if (cinemaId !== user.cinema.toString()) throw new MatchError('You can only modify your cinema') | ||
|
||
return User.findByIdAndUpdate(userId, { $unset: { cinema } }) | ||
|
||
}) | ||
|
||
|
||
}) | ||
|
||
|
||
} | ||
export default deleteCinemaToManager |
15 changes: 15 additions & 0 deletions
15
staff/sergio-ocaña/project/api/logic/deleteCinemaToManager.test.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,15 @@ | ||
import deleteCinemaToManager from './deleteCinemaToManager.js' | ||
|
||
import mongoose from 'mongoose' | ||
|
||
mongoose.connect('mongodb://localhost:27017/project') | ||
.catch(error => console.error(error)) | ||
.then(() => { | ||
try { | ||
deleteCinemaToManager('667d6925fc9e2642ebf52581', '6682f4966b94b0dd9a25382a') | ||
.then(() => console.log('cinema deleted from manager')) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import addCinemaToManager from './addCinemaToManager.js' | ||
import createCinema from './createCinema.js' | ||
import authenticateUser from './authenticateUser.js' | ||
import registerCustomer from './registerCustomer.js' | ||
import registerManager from './registerManager.js' | ||
import retrieveUser from './retrieveUser.js' | ||
import deleteCinema from './deleteCinema.js' | ||
import deleteCinemaToManager from './deleteCinemaToManager.js' | ||
|
||
const logic = { | ||
authenticateUser, | ||
registerCustomer, | ||
registerManager, | ||
retrieveUser, | ||
|
||
addCinemaToManager, | ||
createCinema, | ||
deleteCinema, | ||
deleteCinemaToManager, | ||
} | ||
|
||
export default logic |