| 
									
										
										
										
											2018-05-31 14:29:00 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Email.js controller | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @description: A set of functions called "actions" of the `email` plugin. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   send: async (ctx) => { | 
					
						
							|  |  |  |     // Retrieve provider configuration.
 | 
					
						
							|  |  |  |     const config = await strapi.store({ | 
					
						
							|  |  |  |       environment: strapi.config.environment, | 
					
						
							|  |  |  |       type: 'plugin', | 
					
						
							|  |  |  |       name: 'email' | 
					
						
							|  |  |  |     }).get({ key: 'provider' }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Verify if the file email is enable.
 | 
					
						
							|  |  |  |     if (config.enabled === false) { | 
					
						
							|  |  |  |       strapi.log.error('Email is disabled'); | 
					
						
							|  |  |  |       return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Email.status.disabled' }] }] : 'Emailis disabled'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Something is wrong
 | 
					
						
							|  |  |  |     if (ctx.status === 400) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-18 13:20:55 +02:00
										 |  |  |     let options = ctx.request.body; | 
					
						
							| 
									
										
										
										
											2018-05-31 14:29:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-18 13:20:55 +02:00
										 |  |  |     await strapi.plugins.email.services.email.send(options, config); | 
					
						
							| 
									
										
										
										
											2018-05-31 14:29:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Send 200 `ok`
 | 
					
						
							|  |  |  |     ctx.send({}); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getEnvironments: async (ctx) => { | 
					
						
							|  |  |  |     const environments =  _.map(_.keys(strapi.config.environments), environment => { | 
					
						
							|  |  |  |       return { | 
					
						
							|  |  |  |         name: environment, | 
					
						
							|  |  |  |         active: (strapi.config.environment === environment) | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.send({ environments }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getSettings: async (ctx) => { | 
					
						
							| 
									
										
										
										
											2018-09-18 13:20:55 +02:00
										 |  |  |     let config = await strapi.plugins.email.services.email.getProviderConfig(ctx.params.environment); | 
					
						
							| 
									
										
										
										
											2018-05-31 14:29:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.send({ | 
					
						
							|  |  |  |       providers: strapi.plugins.email.config.providers, | 
					
						
							|  |  |  |       config | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   updateSettings: async (ctx) => { | 
					
						
							|  |  |  |     await strapi.store({ | 
					
						
							|  |  |  |       environment: ctx.params.environment, | 
					
						
							|  |  |  |       type: 'plugin', | 
					
						
							|  |  |  |       name: 'email' | 
					
						
							|  |  |  |     }).set({key: 'provider', value: ctx.request.body}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.send({ok: true}); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; |