Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mailer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
web-et-numerique
web-et-numerique-internet
data.grandlyon.com
web-portal
components
services
mailer
Commits
0a8fd155
There was a problem fetching the pipeline summary.
Commit
0a8fd155
authored
6 years ago
by
FORESTIER Fabien
Browse files
Options
Downloads
Patches
Plain Diff
Improving error handling
parent
541a5844
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Pipeline
#
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
docker-compose.yml
+1
-1
1 addition, 1 deletion
docker-compose.yml
src/email/email.controller.ts
+9
-3
9 additions, 3 deletions
src/email/email.controller.ts
src/email/email.service.ts
+23
-16
23 additions, 16 deletions
src/email/email.service.ts
with
33 additions
and
20 deletions
docker-compose.yml
+
1
−
1
View file @
0a8fd155
...
...
@@ -16,7 +16,7 @@ services:
environment
:
-
RABBITMQ_DEFAULT_USER=${RABBITMQ_USERNAME}
-
RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD}
-
EMAIL_CONTACT=${EMAIL_CONTACT
}
-
ADMIN_EMAIL=${ADMIN_EMAIL
}
volumes
:
-
rabbitmqPersistence:/var/lib/rabbitmq
restart
:
unless-stopped
...
...
This diff is collapsed.
Click to expand it.
src/email/email.controller.ts
+
9
−
3
View file @
0a8fd155
import
{
Controller
,
Post
,
Body
}
from
'
@nestjs/common
'
;
import
{
Controller
,
Post
,
Body
,
Res
}
from
'
@nestjs/common
'
;
import
{
ContactForm
}
from
'
./email
'
;
import
{
EmailService
}
from
'
./email.service
'
;
import
{
ApiBadRequestResponse
,
ApiOkResponse
}
from
'
@nestjs/swagger
'
;
...
...
@@ -13,8 +13,14 @@ export class EmailController {
@
Post
(
'
contact
'
)
@
ApiOkResponse
({
description
:
'
OK
'
})
@
ApiBadRequestResponse
({
description
:
'
Missing fields
'
})
create
(@
Body
()
contactForm
:
ContactForm
)
{
return
this
.
emailService
.
send
(
contactForm
);
create
(@
Body
()
contactForm
:
ContactForm
,
@
Res
()
res
)
{
const
created
=
this
.
emailService
.
send
(
contactForm
);
if
(
created
===
true
)
{
res
.
status
(
200
).
send
();
}
else
{
res
.
status
(
400
).
send
({
error
:
'
Couldn
\'
t send the email
'
});
}
}
}
This diff is collapsed.
Click to expand it.
src/email/email.service.ts
+
23
−
16
View file @
0a8fd155
...
...
@@ -5,39 +5,46 @@ import { ContactForm, Email } from './email';
@
Injectable
()
export
class
EmailService
{
send
(
contactForm
:
ContactForm
)
{
send
(
contactForm
:
ContactForm
)
:
boolean
{
const
rabbitmqUrl
=
'
amqp://user:password123@rabbitmq:5672
'
;
const
mailerQueue
=
'
portail-data-send-email
'
;
const
email
=
new
Email
();
email
.
from
=
`
${
contactForm
.
firstname
}
${
contactForm
.
lastname
}
${
contactForm
.
from
}
`
;
email
.
to
=
[
process
.
env
.
EMAIL_CONTACT
];
email
.
to
=
[
process
.
env
.
ADMIN_EMAIL
];
email
.
subject
=
contactForm
.
subject
;
email
.
text
=
contactForm
.
text
;
Logger
.
log
(
email
);
// Connect to rabbitmq
amqp
.
connect
(
rabbitmqUrl
,
(
err
,
conn
)
=>
{
if
(
err
!=
null
)
{
Logger
.
log
(
err
);
}
// Create a communication channel
conn
.
createChannel
((
error
,
ch
)
=>
{
return
amqp
.
connect
(
rabbitmqUrl
,
(
err
,
conn
)
=>
{
if
(
err
!=
null
)
{
Logger
.
log
(
err
);
return
false
;
}
else
{
// Create a communication channel
conn
.
createChannel
((
error
,
ch
)
=>
{
if
(
error
!=
null
)
Logger
.
log
(
error
);
// Stringify and bufferise message
const
buffer
=
Buffer
.
from
(
JSON
.
stringify
(
email
));
if
(
error
!=
null
)
{
Logger
.
log
(
error
);
return
false
;
}
else
{
// Stringify and bufferise message
const
buffer
=
Buffer
.
from
(
JSON
.
stringify
(
email
));
ch
.
assertQueue
(
mailerQueue
,
{
durable
:
true
});
ch
.
assertQueue
(
mailerQueue
,
{
durable
:
true
});
ch
.
sendToQueue
(
mailerQueue
,
buffer
,
{
persistent
:
true
});
ch
.
sendToQueue
(
mailerQueue
,
buffer
,
{
persistent
:
true
});
Logger
.
log
(
`sent to queue
${
mailerQueue
}
:
${
JSON
.
stringify
(
email
)}
`
);
Logger
.
log
(
`sent to queue
${
mailerQueue
}
:
${
JSON
.
stringify
(
email
)}
`
);
}
);
conn
.
close
(
);
setTimeout
(()
=>
{
conn
.
close
();
},
500
);
return
true
;
}
});
}
});
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment