public async Task <BaseResult> CreateOrUpdate(EmailTemplateItem emailTemplateItem)
        {
            var emailTemplate = emailTemplateItem.ToEmailTemplate();
            var rs            = new BaseResult()
            {
                Result = Result.Success
            };

            return(emailTemplate.Id <= 0 ? await Create(emailTemplate) : await Update(emailTemplate));
        }
        /// <summary>
        /// Prepares to, cc and subject for accreditation outcome email
        /// </summary>
        /// <param name="outcomeLevel"></param>
        /// <param name="organizationId"></param>
        /// <param name="appUniqueId"></param>
        /// <returns></returns>
        public EmailTemplateItem GetAccrediationEmailItems(string outcomeLevel, int organizationId, string appUniqueId, string url)
        {
            var accreditationOutcomeManager     = this.container.GetInstance <AccreditationOutcomeManager>();
            var organizationManager             = this.container.GetInstance <OrganizationManager>();
            var applicationManager              = this.container.GetInstance <ApplicationManager>();
            var applicationSettingManager       = this.container.GetInstance <ApplicationSettingManager>();
            var inspectionScheduleDetailManager = this.container.GetInstance <InspectionScheduleDetailManager>();
            var emailTemplateManager            = this.container.GetInstance <EmailTemplateManager>();
            var userManager       = this.container.GetInstance <UserManager>();
            var emailTemplateItem = new EmailTemplateItem();
            var subject           = string.Empty;
            var emailTemplateName = string.Empty;
            var body   = string.Empty;
            var ccList = new List <string>();
            var toList = new List <string>();

            emailTemplateName = outcomeLevel == Constants.OutcomeStatus.Level1 ? Constants.EmailTemplateName.AccreditationLevel1 : Constants.EmailTemplateName.AccreditationOther;
            var outcomeEmailTemplete = emailTemplateManager.GetByName(emailTemplateName);
            var organization         = organizationManager.GetById(organizationId);
            var application          = applicationManager.GetByUniqueId(new Guid(appUniqueId));

            if (outcomeEmailTemplete == null)
            {
                throw new Exception("Cannot Find the Accredition Outcome Email Template");
            }

            body = PrepareOutcomeEmail(outcomeEmailTemplete.Html, organization, application, outcomeLevel, url);

            var factEmail           = applicationSettingManager.GetByName(Constants.ApplicationSettings.AutoCcEmailAddress);
            var orgFacilityDirector = organization.OrganizationFacilities.Select(x => x.Facility).Select(y => y.FacilityDirector?.EmailAddress).ToList();
            var orgPrimaryContact   = organization.PrimaryUser.EmailAddress;

            subject = outcomeLevel == Constants.OutcomeStatus.Level1 ? "FACT Accreditation Awarded - " + organization.Name : Constants.EmailTemplates.AccreditationOutcomeSubject + organization.Name;

            foreach (var director in organization.Users.Where(x => x.JobFunctionId == Constants.JobFunctionIds.OrganizationDirector))
            {
                toList.Add(director.User.EmailAddress);
            }

            ccList.Add(factEmail?.Value ?? "*****@*****.**");

            if (orgFacilityDirector.Count() > 0)
            {
                ccList.AddRange(orgFacilityDirector.Distinct());
            }

            if (!string.IsNullOrEmpty(orgPrimaryContact))
            {
                ccList.Add(orgPrimaryContact);
            }

            if (!string.IsNullOrEmpty(organization.CcEmailAddresses))
            {
                var add = organization.CcEmailAddresses.Split(';');

                ccList.AddRange(add.ToList());
            }

            //if (factCoordinators.Count() > 0)
            //    ccList.AddRange(factCoordinators.Distinct());

            if (application.ComplianceApplicationId.HasValue)
            {
                ccList.Add(application.ComplianceApplication.Coordinator.EmailAddress);
            }
            else if (application?.CoordinatorId != null)
            {
                ccList.Add(application.Coordinator.EmailAddress);
            }

            if (outcomeLevel == Constants.OutcomeStatus.Level1)
            {
                var inspectionScheduleDetail = inspectionScheduleDetailManager.GetAllActiveByApplication(new Guid(appUniqueId));
                var inspectionTeam           = inspectionScheduleDetail.Select(x => x.User.EmailAddress).ToList();

                if (inspectionTeam.Count() > 0)
                {
                    ccList.AddRange(inspectionTeam);
                }
            }

            emailTemplateItem.To      = String.Join(", ", toList);
            emailTemplateItem.Cc      = String.Join(", ", ccList);
            emailTemplateItem.Subject = subject;
            emailTemplateItem.Html    = body;

            return(emailTemplateItem);
        }
        public async Task <IActionResult> CreateOrUpdate([FromBody] EmailTemplateItem template)
        {
            var result = await _emailTemplateService.CreateOrUpdate(template);

            return(Ok(result));
        }
 public static EmailTemplate ToEmailTemplate(this EmailTemplateItem entity)
 {
     return(entity.MapTo <EmailTemplateItem, EmailTemplate>());
 }