-
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.
This reverts commit 7270354.
- Loading branch information
Showing
9 changed files
with
241 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -70,7 +70,7 @@ describe('DELETE /api/v1/subscribe/delete/:id', () => { | |
await subscribeRepository.save(subscription); | ||
|
||
const response = await request(app) | ||
.delete(`/api/v1/subscribe/delete/${subscription.id}`) | ||
.get(`/api/v1/subscribe/delete/${subscription.id}`) | ||
.send(); | ||
|
||
expect(response.status).toBe(200); | ||
|
@@ -79,7 +79,7 @@ describe('DELETE /api/v1/subscribe/delete/:id', () => { | |
|
||
it('should return 404 if the subscription does not exist', async () => { | ||
const response = await request(app) | ||
.delete('/api/v1/subscribe/delete/450') | ||
.get('/api/v1/subscribe/delete/450') | ||
.send(); | ||
|
||
expect(response.status).toBe(404); | ||
|
@@ -88,10 +88,37 @@ describe('DELETE /api/v1/subscribe/delete/:id', () => { | |
|
||
it('should return 400 for invalid ID', async () => { | ||
const response = await request(app) | ||
.delete('/api/v1/subscribe/delete/noid') | ||
.get('/api/v1/subscribe/delete/noid') | ||
.send(); | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.body.message).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('GET /api/v1/subscribe/getAll', () => { | ||
beforeEach(async () => { | ||
await subscribeRepository.clear(); | ||
}); | ||
|
||
it('should return all subscriptions', async () => { | ||
const subscription1 = new Subscription(); | ||
subscription1.email = '[email protected]'; | ||
await subscribeRepository.save(subscription1); | ||
|
||
const subscription2 = new Subscription(); | ||
subscription2.email = '[email protected]'; | ||
await subscribeRepository.save(subscription2); | ||
|
||
const response = await request(app).get('/api/v1/subscribe/getAll').send(); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body.subscription).toHaveLength(2); | ||
expect(response.body.subscription).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ email: '[email protected]' }), | ||
expect.objectContaining({ email: '[email protected]' }), | ||
]) | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -43,7 +43,7 @@ | |
/** | ||
* @openapi | ||
* /api/v1/subscribe/delete/{id}: | ||
* delete: | ||
* get: | ||
* tags: [Subscribe] | ||
* summary: Removes a user from subscription | ||
* parameters: | ||
|
@@ -85,3 +85,36 @@ | |
* type: string | ||
* example: Invalid ID | ||
*/ | ||
|
||
/** | ||
* @openapi | ||
* /api/v1/subscribe/getAll: | ||
* get: | ||
* tags: [Subscribe] | ||
* summary: Retrieve all subscribers | ||
* description: Get a list of all subscribers in the system. | ||
* responses: | ||
* 200: | ||
* description: A list of subscribers | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: array | ||
* items: | ||
* type: object | ||
* properties: | ||
* id: | ||
* type: integer | ||
* example: 1 | ||
* email: | ||
* type: string | ||
* example: [email protected] | ||
* createdAt: | ||
* type: string | ||
* format: date-time | ||
* example: 2024-06-29T12:34:56Z | ||
* updatedAt: | ||
* type: string | ||
* format: date-time | ||
* example: 2024-06-29T12:34:56Z | ||
*/ |
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,62 @@ | ||
import axios from 'axios'; | ||
import handlebars from 'handlebars'; | ||
import fs from 'fs'; | ||
import dotenv from 'dotenv'; | ||
dotenv.config(); | ||
type EmailType = 'subscribe' | 'unsubscribe'; | ||
type Data = { | ||
name: string; | ||
link: string; | ||
}; | ||
/** | ||
* Sends an email of the specified type to the recipient using the provided data. | ||
* | ||
* @param emailType - The type of email to send. Must be either "confirm" or "reset". | ||
* @param recipient - The email address of the recipient. | ||
* @param data - The data to be used for generating the email content. A name and link are required. | ||
* @returns A Promise that resolves to the response from the email service. | ||
* @throws An error if there is an issue sending the email. | ||
*/ | ||
async function sendEmail(emailType: EmailType, recipient: string, data: Data) { | ||
const templatePath = `./src/emails/templates/${emailType}.html`; | ||
try { | ||
// Read the Handlebars template file | ||
const templateFile = fs.readFileSync(templatePath, 'utf-8'); | ||
|
||
// Compile the template | ||
const template = handlebars.compile(templateFile); | ||
|
||
// Generate the HTML content using the template and data | ||
const html = template(data); | ||
|
||
// Send the Email | ||
|
||
const domain = process.env.MAILGUN_DOMAIN; | ||
const key = process.env.MAILGUN_TOKEN as string; | ||
const body = { | ||
from: `Dynamites Account Team <info@${domain}>`, | ||
to: [recipient], | ||
subject: 'Verification Email', | ||
html: html, | ||
}; | ||
const mailgunResponse = await axios.post( | ||
`https://api.mailgun.net/v3/${domain}/messages`, | ||
body, | ||
{ | ||
auth: { | ||
username: 'api', | ||
password: key, | ||
}, | ||
headers: { | ||
'Content-Type': 'multipart/form-data', | ||
}, | ||
} | ||
); | ||
|
||
return mailgunResponse; | ||
} catch (error) { | ||
throw new Error(`Error sending email: ${error}`); | ||
} | ||
} | ||
|
||
export default sendEmail; |
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,71 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Welcome to Our Newsletter</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
line-height: 1.6; | ||
background-color: #f4f4f4; | ||
color: #333; | ||
padding: 20px; | ||
} | ||
.container { | ||
max-width: 600px; | ||
margin: 0 auto; | ||
background: #fff; | ||
padding: 20px; | ||
border-radius: 10px; | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | ||
} | ||
.header { | ||
text-align: center; | ||
padding-bottom: 20px; | ||
} | ||
.content { | ||
text-align: center; | ||
} | ||
.footer { | ||
text-align: center; | ||
margin-top: 20px; | ||
font-size: 12px; | ||
color: #777; | ||
} | ||
.link { | ||
color: #4c51bf; | ||
text-decoration: none; | ||
} | ||
|
||
.link:hover { | ||
color: #434190; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="header"> | ||
<h1>Welcome to Our Newsletter!</h1> | ||
</div> | ||
<div class="content"> | ||
<p>Hi there,</p> | ||
<p> | ||
Thank you for subscribing to our newsletter. We're excited to have you | ||
with us! | ||
</p> | ||
<p> | ||
You'll receive regular updates on our latest content, news, and | ||
special offers. | ||
</p> | ||
</div> | ||
<div class="footer"> | ||
<p>© 2024 Dynamites Ecommerce. All rights reserved.</p> | ||
<p> | ||
If you didn't subscribe to this newsletter, please | ||
<a href="{{link}}" class="link">unsubscribe</a> | ||
</p> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
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