-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
572f66c
commit 9cd4720
Showing
6 changed files
with
171 additions
and
1 deletion.
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 Message = require('../model/messages'); | ||
const { validateContactUsSchema } = require('../middleware/validators'); | ||
|
||
exports.contactUs = async (req, res) => { | ||
try { | ||
const { error, value } = validateContactUsSchema(req.body); | ||
if (error) return res.status(400).send(error.details); | ||
const { first_name, last_name, email, message, additional_details } = value; | ||
|
||
const newMessage = await Message.create([ | ||
{ | ||
first_name: first_name, | ||
last_name: last_name, | ||
email: email, | ||
message: message, | ||
additional_details: additional_details | ||
} | ||
]); | ||
|
||
await newMessage[0].save(); | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
message: 'Message sent successfully' | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
success: false, | ||
message: 'Unable to send message', | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const messageSchema = new mongoose.Schema({ | ||
first_name: { | ||
type: String, | ||
required: [true, 'Please enter your First name'], | ||
trim: true | ||
}, | ||
|
||
last_name: { | ||
type: String, | ||
required: [true, 'Please enter your Last name'], | ||
trim: true | ||
}, | ||
email: { | ||
type: String, | ||
required: [true, 'Please enter Email address'], | ||
trim: true, | ||
unique: true, | ||
lowercase: true, | ||
match: [ | ||
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, | ||
'Please enter a valid Email address' | ||
] | ||
}, | ||
message: { | ||
type: String, | ||
required: true | ||
}, | ||
additional_details: { | ||
type: String, | ||
required: false | ||
} | ||
}); | ||
|
||
const Message = mongoose.model('Message', messageSchema); | ||
|
||
module.exports = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
|
||
const { contactUs } = require('../controllers/message'); | ||
|
||
router.post('/', contactUs); | ||
|
||
module.exports = router; |
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,79 @@ | ||
/** | ||
* @openapi | ||
* /contact-us: | ||
* post: | ||
* tags: | ||
* - Message | ||
* summary: Contact us | ||
* description: Contact us | ||
* requestBody: | ||
* description: Contact us | ||
* required: true | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* first_name: | ||
* type: string | ||
* required: true | ||
* last_name: | ||
* type: string | ||
* required: true | ||
* email: | ||
* type: string | ||
* format: email | ||
* required: true | ||
* message: | ||
* type: string | ||
* required: true | ||
* additional_details: | ||
* type: string | ||
* required: false | ||
* responses: | ||
* 200: | ||
* description: Message sent successfully | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* success: | ||
* type: boolean | ||
* example: true | ||
* message: | ||
* type: string | ||
* example: Message sent successfully | ||
* 400: | ||
* description: Bad request | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* success: | ||
* type: boolean | ||
* example: false | ||
* message: | ||
* type: string | ||
* example: Please enter a valid Email address | ||
* error: | ||
* type: object | ||
* example: { details: [{ message: "Please enter a valid Email address", path: ["email"], type: "string.email" }] } | ||
* 500: | ||
* description: Internal Server Error | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* success: | ||
* type: boolean | ||
* example: false | ||
* message: | ||
* type: string | ||
* example: Unable to send message | ||
* error: | ||
* type: object | ||
* example: { message: "Unable to send message" } | ||
*/ |