-
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 some examples from presentation slides
- Loading branch information
Gael
committed
Dec 7, 2018
1 parent
b1bc499
commit faf79d7
Showing
4 changed files
with
87 additions
and
0 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,33 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const Schema = mongoose.Schema; | ||
|
||
const UserSchema = new Schema({ | ||
name: { type: String, default: '' }, | ||
email: { type: String, default: '' }, | ||
role: { type: String, enum: ['USER', 'ADMIN'] }, | ||
}); | ||
|
||
UserSchema.pre('save', function(next) { | ||
if (!this.isNew) return next(); | ||
|
||
if (this.role != 'ADMIN' && this.email.endsWith('@admins.com')) { | ||
next(new Error('Only admins can use @admins.com emails')); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
|
||
UserSchema.methods = { | ||
isAdmin: function() { | ||
return this.role === 'ADMIN'; | ||
}, | ||
}; | ||
|
||
UserSchema.statics = { | ||
findByEmail: function(email) { | ||
return this.findOne({ email }); | ||
}, | ||
}; | ||
|
||
mongoose.model('User', UserSchema); |
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 @@ | ||
/src | ||
/controllers | ||
/user.js | ||
/company.js | ||
/product.js | ||
/cart.js | ||
/views | ||
/user.js | ||
/company.js | ||
/product.js | ||
/cart.js | ||
/models | ||
/user.js | ||
/company.js | ||
/product.js | ||
/cart.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,16 @@ | ||
const dbClient = require('dbClient'); | ||
const notificationService = require('notificationService'); | ||
|
||
async function someUseCase() { | ||
const account = await dbClient.collection('accounts').findOne({ | ||
_id: 'someId', | ||
}); | ||
|
||
if (account.type === 'USER') { | ||
notificationService.sendNotification(account); | ||
} | ||
} | ||
|
||
module.exports = { | ||
someUseCase, | ||
}; |
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,18 @@ | ||
module.exports = ({ | ||
accountCollection: { findAccount }, | ||
notificationService: { sendNotification }, | ||
}) => { | ||
async function someUseCase() { | ||
const account = await findAccount({ | ||
_id: 'someId', | ||
}); | ||
|
||
if (account.type === 'USER') { | ||
sendNotification(account); | ||
} | ||
} | ||
|
||
return { | ||
someUseCase, | ||
}; | ||
}; |