protected void SetupDataSharedAcrossTests(Agency agency, Country country,
                                                  Cost cost, CostStageRevision latestRevision, Project project, CostUser costOwner, Guid costOwnerId, CostStage costStage,
                                                  Brand brand, Guid costId, Guid costStageRevisionId, Guid projectId, string budgetRegion = Constants.BudgetRegion.AsiaPacific)
        {
            agency.Country  = country;
            cost.CostNumber = CostNumber;
            cost.LatestCostStageRevision = latestRevision;
            cost.Project             = project;
            costOwner.Agency         = agency;
            costOwner.Id             = costOwnerId;
            latestRevision.CostStage = costStage;
            project.Brand            = brand;

            agency.Name           = AgencyName;
            brand.Name            = BrandName;
            cost.Id               = costId;
            costStage.Name        = CostStageName.ToString();
            costOwner.FullName    = CostOwnerFullName;
            costOwner.GdamUserId  = CostOwnerGdamUserId;
            latestRevision.Id     = costStageRevisionId;
            project.Id            = projectId;
            project.Name          = ProjectName;
            project.GdamProjectId = ProjectGdamId;
            project.AdCostNumber  = ProjectNumber;
            country.Name          = AgencyLocation;

            var stageDetails = new PgStageDetailsForm
            {
                ContentType = new core.Builders.DictionaryValue
                {
                    Id  = Guid.NewGuid(),
                    Key = ContentType
                },
                CostType       = cost.CostType.ToString(),
                ProductionType = new core.Builders.DictionaryValue
                {
                    Id  = Guid.NewGuid(),
                    Key = CostProductionType
                },
                Title = CostTitle,
                AgencyTrackingNumber = AgencyTrackingNumber,
                BudgetRegion         = new AbstractTypeValue
                {
                    Key  = budgetRegion,
                    Name = budgetRegion
                }
            };
            var existingUser = EFContext.CostUser.FirstOrDefault(a => a.GdamUserId == CostOwnerGdamUserId);

            if (existingUser == null)
            {
                EFContext.Add(costOwner);
                EFContext.SaveChanges();
            }

            CostStageRevisionServiceMock.Setup(csr => csr.GetStageDetails <PgStageDetailsForm>(costStageRevisionId)).ReturnsAsync(stageDetails);
        }
        protected void MapEmailNotificationObject(CostNotificationObject obj, Cost cost, CostUser costOwner, CostUser costApprover = null)
        {
            var users = new CostNotificationUsers
            {
                CostOwner = costOwner,
                Approver  = costApprover
            };

            MapEmailNotificationObject(obj, cost, users);
        }
Exemple #3
0
        private async Task AddApproverNotification(List <EmailNotificationMessage <CostNotificationObject> > notifications,
                                                   Cost cost, CostUser costOwner, CostStageRevision costStageRevision, DateTime timestamp, string actionType, CostUser user)
        {
            var notificationMessage = new EmailNotificationMessage <CostNotificationObject>(actionType, user.GdamUserId);

            AddSharedTo(notificationMessage);
            MapEmailNotificationObject(notificationMessage.Object, cost, costOwner, user);
            await PopulateOtherFieldsForRecall(notificationMessage, Constants.EmailNotificationParents.Approver, timestamp, cost.Id, costStageRevision.Id);
            await PopulateMetadata(notificationMessage.Object, cost.Id);

            notifications.Add(notificationMessage);
        }
Exemple #4
0
        public async Task <IEnumerable <EmailNotificationMessage <CostNotificationObject> > > BuildCostReopenRequestedNotification(
            CostNotificationUsers costUsers, Cost cost, CostStageRevision costStageRevision, DateTime timestamp, CostUser requestedBy
            )
        {
            if (cost == null)
            {
                Logger.Error("Failed to create an email notification because cost is null.");
                throw new ArgumentNullException(nameof(cost));
            }

            if (costStageRevision == null)
            {
                Logger.Error("Failed to create an email notification because costStageRevision is null.");
                throw new ArgumentNullException(nameof(costStageRevision));
            }

            if (costUsers.ClientAdmins == null || !costUsers.ClientAdmins.Any())
            {
                Logger.Error("Failed to create an email notification because costUsers is null or empty.");
                throw new ArgumentNullException(nameof(costUsers.ClientAdmins));
            }

            if (requestedBy == null)
            {
                Logger.Error("Failed to create an email notification because rejectedBy is null.");
                throw new ArgumentNullException(nameof(requestedBy));
            }

            if (timestamp == DateTime.MinValue)
            {
                Logger.Error("Failed to create an email notification because system timestamp provided was DateTime.MinValue");
                throw new ArgumentException("Param cannot be DateTime.MinValue", nameof(timestamp));
            }

            if (timestamp == DateTime.MaxValue)
            {
                Logger.Error("Failed to create an email notification because system timestamp provided was DateTime.MaxValue");
                throw new ArgumentException("Param cannot be DateTime.MaxValue", nameof(timestamp));
            }

            var notificationBuilder = new ReopenRequestedNotificationBuilder(_mapper,
                                                                             _uriHelper, _costStageRevisionService, _metadataProviderService, _appSettings, _efContext, _regionsService);

            return(await notificationBuilder.Build(cost, costUsers, requestedBy.FullName, costStageRevision, timestamp));
        }
Exemple #5
0
        public async Task Create_Notification_For_Coupa_For_Non_NA_Non_Cyclone_Agency_When_Cost_Total_Amount_Decreased()
        {
            //Arrange
            decimal?previousPaymentAmount = 10.00M;
            decimal?latestPaymentAmount   = 5.00M; //Decreased from 10
            var     timestamp             = DateTime.UtcNow;

            var cost           = new Cost();
            var costOwner      = new CostUser();
            var latestRevision = new CostStageRevision();

            var previousRevisionId = Guid.NewGuid();
            var latestRevisionId   = Guid.NewGuid();

            SetupPurchaseOrderCost(cost, latestRevision, costOwner, previousRevisionId, latestRevisionId, PoNumber);

            var costUsers = new CostNotificationUsers
            {
                CostOwner = costOwner
            };

            var previousPaymentResult = new PaymentAmountResult
            {
                TotalCostAmount = previousPaymentAmount
            };
            var latestPaymentResult = new PaymentAmountResult
            {
                TotalCostAmount = latestPaymentAmount
            };

            PgPaymentServiceMock.Setup(ppsm => ppsm.GetPaymentAmount(previousRevisionId, false)).Returns(Task.FromResult(previousPaymentResult));
            PgPaymentServiceMock.Setup(ppsm => ppsm.GetPaymentAmount(latestRevisionId, false)).Returns(Task.FromResult(latestPaymentResult));

            //Act
            IEnumerable <EmailNotificationMessage <CostNotificationObject> > result = await EmailNotificationBuilder.BuildPendingBrandApprovalNotification(costUsers, cost,
                                                                                                                                                           latestRevision, timestamp);

            //Assert
            result.Should().NotBeNull();
            var notifications = result.ToArray();

            notifications.Should().HaveCount(1);
        }
        internal async Task <IEnumerable <EmailNotificationMessage <CostNotificationObject> > > Build(
            Cost cost,
            CostNotificationUsers costUsers,
            CostStageRevision costStageRevision,
            DateTime timestamp,
            CostUser changeApprover,
            CostUser previousOwner)
        {
            var    notifications = new List <EmailNotificationMessage <CostNotificationObject> >();
            string actionType    = core.Constants.EmailNotificationActionType.CostOwnerChanged;
            var    costOwner     = costUsers.CostOwner;

            //add recipients
            var recipients = new List <string> {
                costOwner.GdamUserId
            };

            if (costUsers.Watchers != null)
            {
                recipients.AddRange(costUsers.Watchers);
            }
            if (costUsers.Approvers != null)
            {
                recipients.AddRange(costUsers.Approvers);
            }
            recipients.Add(previousOwner.GdamUserId);

            var costOwnerNotification = new EmailNotificationMessage <CostNotificationObject>(actionType, recipients.Distinct());

            AddSharedTo(costOwnerNotification);
            MapEmailNotificationObject(costOwnerNotification.Object, cost, costOwner);
            costOwnerNotification.Object.Cost.PreviousOwner = previousOwner.FullName;
            var approver = costOwnerNotification.Object.Approver;

            approver.Name = changeApprover.FullName;
            PopulateOtherFields(costOwnerNotification, core.Constants.EmailNotificationParents.CostOwner, timestamp, cost.Id, costStageRevision.Id);
            await PopulateMetadata(costOwnerNotification.Object, cost.Id);

            notifications.Add(costOwnerNotification);

            return(notifications);
        }
        protected void MapEmailNotificationObject(CostNotificationObject obj, Cost cost, CostNotificationUsers users)
        {
            dataAccess.Entity.Project project = cost.Project;

            obj.Agency = Mapper.Map <core.Models.Notifications.Agency>(users.CostOwner.Agency);
            Mapper.Map(users.CostOwner.Agency.Country, obj.Agency);

            obj.Brand = Mapper.Map <core.Models.Notifications.Brand>(project.Brand);

            obj.Cost = Mapper.Map <core.Models.Notifications.Cost>(cost);
            Mapper.Map(cost.LatestCostStageRevision.CostStage, obj.Cost);
            Mapper.Map(users.CostOwner, obj.Cost);

            obj.Project = Mapper.Map <core.Models.Notifications.Project>(project);

            if (users.Approver != null)
            {
                obj.Approver = Mapper.Map <Approver>(users.Approver);
            }
        }
        private Cost MockCost()
        {
            var costId = Guid.NewGuid();

            var costNumber  = "AC" + (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds;
            var createdById = Guid.NewGuid();
            var cost        = new Cost
            {
                Id          = costId,
                CostNumber  = costNumber,
                CreatedById = createdById,
                OwnerId     = createdById
            };

            _efContextMock.MockAsyncQueryable(new List <Cost> {
                cost
            }.AsQueryable(), c => c.Cost);

            return(cost);
        }
        protected async Task <EmailNotificationMessage <CostNotificationObject> > AddFinanceManagerNotification(
            string actionType,
            Cost cost,
            CostNotificationUsers costUsers,
            CostStageRevision costStageRevision,
            DateTime timestamp,
            List <EmailNotificationMessage <CostNotificationObject> > notifications
            )
        {
            //Only send to North American P&G Finance Users when Budget Region is North America and Cyclone costs
            if (costUsers.FinanceManagementUsers == null ||
                !costUsers.FinanceManagementUsers.Any() ||
                Constants.BudgetRegion.NorthAmerica != GetBudgetRegion(costStageRevision) ||
                !costUsers.CostOwner.Agency.IsCyclone())
            {
                return(null);
            }

            //Send to FinanceManagement as well
            var financeManagementNotification =
                new EmailNotificationMessage <CostNotificationObject>(actionType, costUsers.FinanceManagementUsers);
            var notificationObject = new FinanceManagerCostNotificationObject();

            financeManagementNotification.Object = notificationObject;
            var parent = core.Constants.EmailNotificationParents.FinanceManagement;

            AddSharedTo(financeManagementNotification);
            MapEmailNotificationObject(financeManagementNotification.Object, cost, costUsers);
            PopulateOtherFields(financeManagementNotification, parent, timestamp, cost.Id, costStageRevision.Id);
            await PopulateMetadata(financeManagementNotification.Object, cost.Id);

            notifications.Add(financeManagementNotification);

            if (cost.Status == CostStageRevisionStatus.Approved && cost.LatestCostStageRevision.CostStage.Key == Models.Stage.CostStages.OriginalEstimate.ToString())
            {
                notificationObject.CanAssignIONumber = true;
            }

            return(financeManagementNotification);
        }
Exemple #10
0
        public async Task <IEnumerable <EmailNotificationMessage <CostNotificationObject> > > BuildCostSubmittedNotification(CostNotificationUsers costUsers,
                                                                                                                             Cost cost, CostStageRevision costStageRevision, DateTime timestamp)
        {
            if (cost == null)
            {
                Logger.Error("Failed to create an email notification because cost is null.");
                throw new ArgumentNullException(nameof(cost));
            }

            if (costStageRevision == null)
            {
                Logger.Error("Failed to create an email notification because costStageRevision is null.");
                throw new ArgumentNullException(nameof(costStageRevision));
            }

            if (costUsers == null)
            {
                Logger.Error("Failed to create an email notification because costUsers is null.");
                throw new ArgumentNullException(nameof(costUsers));
            }

            if (timestamp == DateTime.MinValue)
            {
                Logger.Error("Failed to create an email notification because system timestamp provided was DateTime.MinValue");
                throw new ArgumentException("Param cannot be DateTime.MinValue", nameof(timestamp));
            }

            if (timestamp == DateTime.MaxValue)
            {
                Logger.Error("Failed to create an email notification because system timestamp provided was DateTime.MaxValue");
                throw new ArgumentException("Param cannot be DateTime.MaxValue", nameof(timestamp));
            }

            var submittedNotificationBuilder = new SubmittedNotificationBuilder(_mapper,
                                                                                _uriHelper, _costStageRevisionService, _metadataProviderService, _appSettings, _efContext, _regionsService);

            return(await submittedNotificationBuilder.Build(cost, costUsers, costStageRevision, timestamp));
        }
Exemple #11
0
        private void SetupPurchaseOrderCost(Cost cost, CostStageRevision latestRevision,
                                            CostUser costOwner, Guid previousRevisionId, Guid latestRevisionId, string poNumber)
        {
            const string brandApproverGdamUserId = "57e5461ed9563f268ef4f1ta";
            const string brandApproverName       = "John Smith";
            var          costId      = Guid.NewGuid();
            var          costOwnerId = Guid.NewGuid();
            var          projectId   = Guid.NewGuid();

            var previousStageId = Guid.NewGuid();
            var latestStageId   = Guid.NewGuid();

            var previousRevision        = new CostStageRevision();
            var previousStage           = new CostStage();
            var latestStage             = new CostStage();
            var project                 = new Project();
            var brand                   = new Brand();
            var agency                  = new Agency();
            var country                 = new Country();
            var brandApproval           = new Approval();
            var brandApprover           = new ApprovalMember();
            var brandApproverAsCostUser = new CostUser();

            previousRevision.CostStage = previousStage;
            previousRevision.Id        = previousRevisionId;

            previousStage.Id = previousRevision.CostStageId = previousStageId;
            latestStage.Id   = latestRevision.CostStageId = latestStageId;

            previousStage.Name = CostStages.OriginalEstimate.ToString();
            latestStage.Name   = CostStages.FinalActual.ToString();

            cost.CostStages.AddRange(new[] { previousStage, latestStage });

            //China non-Cyclone Agencies should create a notification for non-backup approver when the cost total amount has changed
            SetupDataSharedAcrossTests(agency, country, cost, latestRevision, project, costOwner, costOwnerId, latestStage,
                                       brand, costId, latestRevisionId, projectId, Constants.BudgetRegion.China);

            brandApproval.ApprovalMembers = new List <ApprovalMember> {
                brandApprover
            };
            brandApprover.CostUser = brandApproverAsCostUser;

            brandApproval.Type = ApprovalType.Brand;
            brandApproverAsCostUser.GdamUserId = brandApproverGdamUserId;
            brandApproverAsCostUser.FullName   = brandApproverName;

            var approvals = new List <Approval> {
                brandApproval
            };

            ApprovalServiceMock.Setup(a => a.GetApprovalsByCostStageRevisionId(It.IsAny <Guid>(), true)).ReturnsAsync(approvals);

            var pgPaymentDetails = new PgPaymentDetails
            {
                PoNumber = poNumber
            };

            CostStageServiceMock.Setup(cssm => cssm.GetPreviousCostStage(latestStageId)).Returns(Task.FromResult(previousStage));
            CostStageRevisionServiceMock.Setup(csrsm => csrsm.GetLatestRevision(previousStageId)).Returns(Task.FromResult(previousRevision));
            CustomObjectDataServiceMock
            .Setup(codsm => codsm.GetCustomData <PgPaymentDetails>(latestRevisionId, CustomObjectDataKeys.PgPaymentDetails))
            .Returns(Task.FromResult(pgPaymentDetails));
            CustomObjectDataServiceMock
            .Setup(codsm => codsm.GetCustomData <PgPaymentDetails>(previousRevisionId, CustomObjectDataKeys.PgPaymentDetails))
            .Returns(Task.FromResult(pgPaymentDetails));
        }
Exemple #12
0
        public async Task <IEnumerable <EmailNotificationMessage <CostNotificationObject> > > BuildCostReminderNotification(CostNotificationUsers costUsers,
                                                                                                                            Cost cost, CostStageRevision costStageRevision, DateTime timestamp)
        {
            if (cost == null)
            {
                Logger.Error("Failed to create an email notification because cost is null.");
                throw new ArgumentNullException(nameof(cost));
            }

            if (costStageRevision == null)
            {
                Logger.Error("Failed to create an email notification because costStageRevision is null.");
                throw new ArgumentNullException(nameof(costStageRevision));
            }

            if (costUsers == null)
            {
                Logger.Error("Failed to create an email notification because costUsers is null.");
                throw new ArgumentNullException(nameof(costUsers));
            }

            if (timestamp == DateTime.MinValue)
            {
                Logger.Error("Failed to create an email notification because system timestamp provided was DateTime.MinValue");
                throw new ArgumentException("Param cannot be DateTime.MinValue", nameof(timestamp));
            }

            if (timestamp == DateTime.MaxValue)
            {
                Logger.Error("Failed to create an email notification because system timestamp provided was DateTime.MaxValue");
                throw new ArgumentException("Param cannot be DateTime.MaxValue", nameof(timestamp));
            }

            //ADC-2698 send notification to technical approver
            var notificationMessages = new List <EmailNotificationMessage <CostNotificationObject> >();

            if (costStageRevision.Status == CostStageRevisionStatus.PendingTechnicalApproval)
            {
                var pendingTechnicalNotificationBuilder = new ReminderPendingTechnicalNotificationBuilder(_mapper,
                                                                                                          _uriHelper, _costStageRevisionService, _metadataProviderService, _approvalService, _appSettings, _efContext, _regionsService);
                notificationMessages.AddRange(await pendingTechnicalNotificationBuilder.Build(cost, costUsers, costStageRevision, timestamp));
            }
            else if (costStageRevision.Status == CostStageRevisionStatus.PendingBrandApproval)
            {
                var pendingBrandNotificationBuilder = new ReminderPendingBrandNotificationBuilder(_mapper,
                                                                                                  _uriHelper, _costStageRevisionService, _approvalService, _metadataProviderService, _regionsService, _appSettings, _efContext);
                notificationMessages.AddRange(await pendingBrandNotificationBuilder.Build(cost, costUsers, costStageRevision, timestamp));
            }

            return(notificationMessages);
        }
Exemple #13
0
        public async Task <IEnumerable <EmailNotificationMessage <CostNotificationObject> > > BuildCostApprovedNotification(CostNotificationUsers costUsers,
                                                                                                                            Cost cost,
                                                                                                                            CostStageRevision costStageRevision,
                                                                                                                            string approverName,
                                                                                                                            string approvalType,
                                                                                                                            DateTime timestamp)
        {
            if (cost == null)
            {
                Logger.Error("Failed to create an email notification because cost is null.");
                throw new ArgumentNullException(nameof(cost));
            }

            if (costStageRevision == null)
            {
                Logger.Error("Failed to create an email notification because costStageRevision is null.");
                throw new ArgumentNullException(nameof(costStageRevision));
            }

            if (costUsers == null)
            {
                Logger.Error("Failed to create an email notification because costUsers is null.");
                throw new ArgumentNullException(nameof(costUsers));
            }

            if (approverName == null)
            {
                Logger.Error("ENB001: Failed to create an email notification because approver is null. This could be because IoNumberOwner is empty from Coupa. The IoNumberOwner is the approver for Costs approved in Coupa.");
                throw new ArgumentNullException(nameof(approverName));
            }

            if (timestamp == DateTime.MinValue)
            {
                Logger.Error("Failed to create an email notification because system timestamp provided was DateTime.MinValue");
                throw new ArgumentException("Param cannot be DateTime.MinValue", nameof(timestamp));
            }

            if (timestamp == DateTime.MaxValue)
            {
                Logger.Error("Failed to create an email notification because system timestamp provided was DateTime.MaxValue");
                throw new ArgumentException("Param cannot be DateTime.MaxValue", nameof(timestamp));
            }

            var notifications = new List <EmailNotificationMessage <CostNotificationObject> >();
            var approvedNotificationBuilder = new ApprovedNotificationBuilder(_mapper,
                                                                              _uriHelper, _costStageRevisionService, _metadataProviderService, _appSettings, _efContext, _regionsService);

            notifications.AddRange(await approvedNotificationBuilder.Build(cost, costUsers, approverName, approvalType, costStageRevision, timestamp));

            return(notifications);
        }
Exemple #14
0
        internal async Task <IEnumerable <EmailNotificationMessage <CostNotificationObject> > > BuildAsync(Cost cost, CostNotificationUsers costUsers, CostStageRevision costStageRevision, DateTime timestamp)
        {
            var notifications = new List <EmailNotificationMessage <CostNotificationObject> >();

            //Cost Owner
            var costOwner             = costUsers.CostOwner;
            var actionType            = Constants.EmailNotificationActionType.Recalled;
            var costOwnerNotification = new EmailNotificationMessage <CostNotificationObject>(actionType, costOwner.GdamUserId);

            AddSharedTo(costOwnerNotification);
            MapEmailNotificationObject(costOwnerNotification.Object, cost, costOwner);
            await PopulateOtherFieldsForRecall(costOwnerNotification, Constants.EmailNotificationParents.CostOwner, timestamp, cost.Id, costStageRevision.Id);
            await PopulateMetadata(costOwnerNotification.Object, cost.Id);

            notifications.Add(costOwnerNotification);

            var approvals = await _approvalService.GetApprovalsByCostStageRevisionId(costStageRevision.Id);

            if (approvals == null)
            {
                //No approvals set
                return(notifications);
            }

            var technicalApprovals = approvals.Where(a => a.Type == ApprovalType.IPM).ToArray();

            foreach (var technicalApproval in technicalApprovals)
            {
                foreach (var approvalMember in technicalApproval.ApprovalMembers)
                {
                    //Send notifications to Technical Approver
                    await AddApproverNotification(notifications, cost, costOwner, costStageRevision, timestamp, actionType, approvalMember.CostUser);
                }
            }

            var brandApprovals = approvals.Where(a => a.Type == ApprovalType.Brand).ToArray();

            foreach (var brandApproval in brandApprovals)
            {
                foreach (var approvalMember in brandApproval.ApprovalMembers)
                {
                    //Send notifications to actual Brand Approvers only
                    if (approvalMember.IsSystemApprover())
                    {
                        continue;
                    }

                    await AddApproverNotification(notifications, cost, costOwner, costStageRevision, timestamp, actionType, approvalMember.CostUser);
                }
                if (brandApproval.Requisitioners == null)
                {
                    continue;
                }

                foreach (var requisitioner in brandApproval.Requisitioners)
                {
                    await AddApproverNotification(notifications, cost, costOwner, costStageRevision, timestamp, actionType, requisitioner.CostUser);
                }
            }
            await AddFinanceManagerNotification(actionType, cost, costUsers, costStageRevision, timestamp, notifications);

            return(notifications);
        }
Exemple #15
0
        internal async Task <IEnumerable <EmailNotificationMessage <CostNotificationObject> > > Build(Cost cost,
                                                                                                      CostNotificationUsers costUsers, string approverName, string approvalType, CostStageRevision costStageRevision, DateTime timestamp)
        {
            var notifications = new List <EmailNotificationMessage <CostNotificationObject> >();

            //Cost Owner
            string actionType;
            var    parent = core.Constants.EmailNotificationParents.CostOwner;

            if (approvalType == core.Constants.EmailApprovalType.Brand)
            {
                actionType   = core.Constants.EmailNotificationActionType.BrandApprovalApproved;
                approvalType = await GetBrandManagerRoleLabel();

                if (cost.IsExternalPurchases)
                {
                    parent = core.Constants.EmailNotificationParents.MyPurchases;
                }
            }
            else
            {
                actionType = core.Constants.EmailNotificationActionType.TechnicalApprovalApproved;
            }
            var costOwner  = costUsers.CostOwner;
            var recipients = new List <string> {
                costOwner.GdamUserId
            };

            if (costUsers.Watchers != null)
            {
                recipients.AddRange(costUsers.Watchers);
            }
            var costOwnerNotification = new EmailNotificationMessage <CostNotificationObject>(actionType, recipients);

            AddSharedTo(costOwnerNotification);
            MapEmailNotificationObject(costOwnerNotification.Object, cost, costOwner);
            PopulateOtherFields(costOwnerNotification, parent, timestamp, cost.Id, costStageRevision.Id);
            await PopulateMetadata(costOwnerNotification.Object, cost.Id);

            var approver = costOwnerNotification.Object.Approver;

            approver.Name = approverName;
            approver.Type = approvalType;
            notifications.Add(costOwnerNotification);

            if (cost.Status == CostStageRevisionStatus.Approved)
            {
                if (ShouldNotifyInsuranceUsers(costUsers))
                {
                    //Send to all InsuranceUsers as well
                    var insuranceUsers            = costUsers.InsuranceUsers;
                    var insuranceUserNotification =
                        new EmailNotificationMessage <CostNotificationObject>(core.Constants.EmailNotificationActionType.AllApprovalsApproved, insuranceUsers);
                    parent = core.Constants.EmailNotificationParents.InsuranceUser;
                    AddSharedTo(insuranceUserNotification);
                    MapEmailNotificationObject(insuranceUserNotification.Object, cost, costUsers);
                    PopulateOtherFields(insuranceUserNotification, parent, timestamp, cost.Id, costStageRevision.Id);
                    await PopulateMetadata(insuranceUserNotification.Object, cost.Id);

                    approver      = insuranceUserNotification.Object.Approver;
                    approver.Name = approverName;
                    approver.Type = approvalType;
                    notifications.Add(insuranceUserNotification);
                }

                var financeManagementNotification = await AddFinanceManagerNotification(core.Constants.EmailNotificationActionType.AllApprovalsApproved,
                                                                                        cost, costUsers, costStageRevision, timestamp, notifications);

                if (financeManagementNotification != null)
                {
                    approver      = financeManagementNotification.Object.Approver;
                    approver.Name = approverName;
                    approver.Type = approvalType;
                }
            }

            return(notifications);
        }
        private void SetupDataSharedAcrossTests()
        {
            const string     agencyLocation      = "United Kingdom";
            const string     agencyName          = "Saatchi";
            const string     brandName           = "P&G";
            const string     costNumber          = "P101";
            const CostStages costStageName       = CostStages.OriginalEstimate;
            const string     costOwnerGdamUserId = "57e5461ed9563f268ef4f19d";
            const string     costOwnerFullName   = "Mr Cost Owner";
            const string     projectName         = "Pampers";
            const string     projectGdamId       = "57e5461ed9563f268ef4f19c";
            const string     projectNumber       = "PandG01";

            var projectId = Guid.NewGuid();

            _cost = new Cost();
            var costOwner = new CostUser
            {
                UserBusinessRoles = new List <UserBusinessRole>
                {
                    new UserBusinessRole
                    {
                        BusinessRole = new BusinessRole
                        {
                            Key   = Constants.BusinessRole.FinanceManager,
                            Value = Constants.BusinessRole.FinanceManager
                        }
                    }
                }
            };
            var approverUser = new CostUser
            {
                UserBusinessRoles = new List <UserBusinessRole>
                {
                    new UserBusinessRole
                    {
                        BusinessRole = new BusinessRole
                        {
                            Key   = Constants.BusinessRole.Ipm,
                            Value = Constants.BusinessRole.Ipm
                        }
                    }
                }
            };
            var insuranceUser = new CostUser
            {
                UserBusinessRoles = new List <UserBusinessRole>
                {
                    new UserBusinessRole
                    {
                        BusinessRole = new BusinessRole
                        {
                            Key   = Constants.BusinessRole.InsuranceUser,
                            Value = Constants.BusinessRole.InsuranceUser
                        }
                    }
                }
            };

            var latestRevision = new CostStageRevision();
            var costStage      = new CostStage();
            var project        = new Project();
            var brand          = new Brand();
            var agency         = new Agency();
            var country        = new Country();

            agency.Country                = country;
            approverUser.Id               = _approverUserId;
            _cost.CreatedBy               = costOwner;
            _cost.CreatedById             = _costOwnerId;
            _cost.Owner                   = costOwner;
            _cost.OwnerId                 = _costOwnerId;
            _cost.CostNumber              = costNumber;
            _cost.LatestCostStageRevision = latestRevision;
            _cost.Project                 = project;
            costOwner.Agency              = agency;
            costOwner.Id                  = _costOwnerId;
            insuranceUser.Id              = _insuranceUserId;
            latestRevision.CostStage      = costStage;
            project.Brand                 = brand;

            agency.Name           = agencyName;
            brand.Name            = brandName;
            _cost.Id              = _costId;
            costStage.Name        = costStageName.ToString();
            costOwner.FullName    = costOwnerFullName;
            costOwner.GdamUserId  = costOwnerGdamUserId;
            costOwner.Id          = _costOwnerId;
            latestRevision.Id     = _costStageRevisionId;
            project.Id            = projectId;
            project.Name          = projectName;
            project.GdamProjectId = projectGdamId;
            project.AdCostNumber  = projectNumber;
            country.Name          = agencyLocation;

            var agencies = new List <Agency> {
                agency
            };
            var brands = new List <Brand> {
                brand
            };
            var costs = new List <Cost> {
                _cost
            };
            var costStages = new List <CostStageRevision> {
                latestRevision
            };
            var costUsers = new List <CostUser> {
                approverUser, costOwner, insuranceUser
            };
            var countries = new List <Country> {
                country
            };
            var projects = new List <Project> {
                project
            };

            _efContextMock.MockAsyncQueryable(agencies.AsQueryable(), c => c.Agency);
            _efContextMock.MockAsyncQueryable(brands.AsQueryable(), c => c.Brand);
            _efContextMock.MockAsyncQueryable(costs.AsQueryable(), c => c.Cost);
            _efContextMock.MockAsyncQueryable(costStages.AsQueryable(), c => c.CostStageRevision);
            _efContextMock.MockAsyncQueryable(costUsers.AsQueryable(), c => c.CostUser);
            _efContextMock.MockAsyncQueryable(countries.AsQueryable(), c => c.Country);
            _efContextMock.MockAsyncQueryable(projects.AsQueryable(), c => c.Project);

            _efContextMock.MockAsyncQueryable(new List <NotificationSubscriber>
            {
                new NotificationSubscriber
                {
                    CostId     = _cost.Id,
                    CostUserId = _costOwnerId,
                    CostUser   = costOwner
                }
            }.AsQueryable(), a => a.NotificationSubscriber);

            _stageDetails = new PgStageDetailsForm
            {
                Title        = "Test Title",
                BudgetRegion = new AbstractTypeValue
                {
                    Name = Constants.BudgetRegion.AsiaPacific
                },
                ContentType = new core.Builders.DictionaryValue
                {
                    Value = Constants.ContentType.Audio
                },
                ProductionType = new core.Builders.DictionaryValue
                {
                    Value = Constants.ProductionType.PostProductionOnly
                }
            };
            _costStageRevisionServiceMock.Setup(c => c.GetStageDetails <PgStageDetailsForm>(_cost.LatestCostStageRevision)).Returns(_stageDetails);
        }
        internal async Task <IEnumerable <EmailNotificationMessage <CostNotificationObject> > > Build(IEnumerable <ApprovalMember> removedApprovers,
                                                                                                      dataAccess.Entity.Cost cost, CostNotificationUsers costUsers, CostStageRevision previousRevision, DateTime timestamp)
        {
            var notifications = new List <EmailNotificationMessage <CostNotificationObject> >();

            foreach (var removedMember in removedApprovers)
            {
                if (removedMember.IsSystemApprover())
                {
                    continue;
                }

                var    approvalCostUser = removedMember.CostUser;
                var    costOwner        = costUsers.CostOwner;
                string actionType       = core.Constants.EmailNotificationActionType.ApproverUnassigned;
                string parent           = core.Constants.EmailNotificationParents.Approver;

                var previousApproverNotification = new EmailNotificationMessage <CostNotificationObject>(actionType, approvalCostUser.GdamUserId);
                AddSharedTo(previousApproverNotification);
                MapEmailNotificationObject(previousApproverNotification.Object, cost, costOwner, approvalCostUser);
                PopulateOtherFields(previousApproverNotification, parent, timestamp, cost.Id, previousRevision.Id);
                await PopulateMetadata(previousApproverNotification.Object, cost.Id);

                var replacement = GetReplacement(cost.LatestCostStageRevision, removedMember);

                var approver = previousApproverNotification.Object.Approver;
                approver.Type = GetApprovalType(removedMember.Approval);
                if (replacement != null)
                {
                    approver.Name = replacement.CostUser.FullName;
                }
                notifications.Add(previousApproverNotification);
            }

            return(notifications);
        }
        internal async Task<IEnumerable<EmailNotificationMessage<CostNotificationObject>>> Build(Cost cost,
            CostNotificationUsers costUsers, CostStageRevision costStageRevision, DateTime timestamp)
        {
            var notifications = new List<EmailNotificationMessage<CostNotificationObject>>();

            //Cost Owner
            var costOwner = costUsers.CostOwner;
            var recipients = new List<string> { costOwner.GdamUserId };
            recipients.AddRange(costUsers.Watchers);

            var costOwnerNotification = new EmailNotificationMessage<CostNotificationObject>(Constants.EmailNotificationActionType.Cancelled, recipients);
            AddSharedTo(costOwnerNotification);
            MapEmailNotificationObject(costOwnerNotification.Object, cost, costOwner);
            PopulateOtherFields(costOwnerNotification, Constants.EmailNotificationParents.CostOwner, timestamp, cost.Id, costStageRevision.Id);
            await PopulateMetadata(costOwnerNotification.Object, cost.Id);
            notifications.Add(costOwnerNotification);

            if (ShouldNotifyInsuranceUsers(costUsers))
            {
                var insuranceUserNotification = new EmailNotificationMessage<CostNotificationObject>(Constants.EmailNotificationActionType.Cancelled, costUsers.InsuranceUsers);
                AddSharedTo(insuranceUserNotification);
                MapEmailNotificationObject(insuranceUserNotification.Object, cost, costOwner);
                PopulateOtherFields(insuranceUserNotification, Constants.EmailNotificationParents.InsuranceUser, timestamp, cost.Id, costStageRevision.Id);
                await PopulateMetadata(insuranceUserNotification.Object, cost.Id);
                notifications.Add(insuranceUserNotification);
            }
            await AddFinanceManagerNotification(Constants.EmailNotificationActionType.Cancelled,
                cost, costUsers, costStageRevision, timestamp, notifications);

            return notifications;
        }
Exemple #19
0
        private void AddMetadataItems(Cost cost, CostUser costOwner, PgStageDetailsForm stageDetails, ICollection <MetadataItem> metadataItems)
        {
            var project       = cost.Project;
            var agency        = costOwner.Agency;
            var agencyCountry = agency.Country;

            // The items below are defined in ADC-2265. The order here is the same order they will appear in the email.
            metadataItems.Add(new MetadataItem
            {
                Label = "Cost Title",
                Value = stageDetails.Title
            });
            metadataItems.Add(new MetadataItem
            {
                Label = "Cost ID",
                Value = cost.CostNumber
            });

            AddCostTypeItems(metadataItems, stageDetails);

            metadataItems.Add(new MetadataItem
            {
                Label = "Status",
                Value = cost.Status.ToStringCustom().AddSpacesToSentence()
            });
            if (stageDetails.AgencyProducer != null)
            {
                metadataItems.Add(new MetadataItem
                {
                    Label = "Agency Producer",
                    Value = string.Join(',', stageDetails.AgencyProducer)
                });
            }
            metadataItems.Add(new MetadataItem
            {
                Label = "Agency Owner",
                Value = $"{costOwner.FullName} - {costOwner.Email}"
            });
            metadataItems.Add(new MetadataItem
            {
                Label = "Agency Name",
                Value = agency.Name
            });
            metadataItems.Add(new MetadataItem
            {
                Label = "Agency Location",
                Value = agencyCountry.Name
            });
            metadataItems.Add(new MetadataItem
            {
                Label = "Agency Tracking #",
                Value = stageDetails.AgencyTrackingNumber
            });
            metadataItems.Add(new MetadataItem
            {
                Label = "Project Name",
                Value = project.Name
            });
            metadataItems.Add(new MetadataItem
            {
                Label = "Project ID",
                Value = project.AdCostNumber
            });
            metadataItems.Add(new MetadataItem
            {
                Label = "Budget Region",
                Value = stageDetails.BudgetRegion.Name
            });

            AddApprovalTypeItems(metadataItems, cost.LatestCostStageRevision.Approvals);

            if (project.Brand != null)
            {
                metadataItems.Add(new MetadataItem
                {
                    Label = "Brand",
                    Value = project.Brand.Name
                });
            }
        }