Exemplo n.º 1
0
        public async Task <APIGatewayProxyResponse> Handler(CustomEventRequest <BasicInput> lambdaEvent, ILambdaContext context)
        {
            //var lunchDate = DateTime.Parse(Environment.GetEnvironmentVariable("lunchTime"));
            // var menuID = Environment.GetEnvironmentVariable("menuID");


            var lunchDate = DateTime.Parse("2020-02-09");
            var menuID    = "5e3c016f214efe00018721ab";


            var service    = new CodeMashRepository <MenuEntity>(Client);
            var menuEntity = await service.FindOneByIdAsync(
                menuID,
                new DatabaseFindOneOptions()
                );

            var menu = new Menu(DateTime.Now, null, null)
            {
                Id       = menuID,
                Division = new Division()
                {
                    Id = menuEntity.DivisionId
                }
            };

            var lunchService = new LunchService()
            {
                HrService = new HrService()
                {
                    EmployeesRepository = new EmployeesRepository()
                },
                MenuRepository     = new MenuRepository(),
                NotificationSender = new NotificationSender()
            };


            await lunchService.AdjustMenuLunchTime(lunchDate, menu, menuEntity.Employees);

            var response = new
            {
                lambdaEvent,
            };

            return(new APIGatewayProxyResponse
            {
                Body = JsonConvert.SerializeObject(response),
                StatusCode = 200,
                Headers = new Dictionary <string, string> {
                    { "Content-Type", "application/json" }
                }
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// (Required) Entry method of your Lambda function.
        /// </summary>
        /// <param name="lambdaEvent">Type returned from CodeMash</param>
        /// <param name="context">Context data of a function (function config)</param>
        /// <returns></returns>
        public async Task <APIGatewayProxyResponse> Handler(CustomEventRequest <CollectionTriggerInput> lambdaEvent, ILambdaContext context)
        {
            var response  = JsonConvert.DeserializeObject <AbsenceRequest>(lambdaEvent.Input.NewRecord);
            var requestId = response.Id;

            #region Initialization

            const string appSettings = "appsettings.json";
            string       projectIdString;
            string       apiKey;

            using (StreamReader reader = new StreamReader(appSettings))
            {
                string jsonString = reader.ReadToEnd();
                var    dataJson   = JObject.Parse(jsonString);
                apiKey          = dataJson["CodeMash"]["ApiKey"].ToString();
                projectIdString = dataJson["CodeMash"]["ProjectId"].ToString();
            }

            var projectId = Guid.Parse(projectIdString);

            // 2. Create a general client for API calls
            var client      = new CodeMashClient(apiKey, projectId);
            var pushService = new CodeMashPushService(client);

            // 3. Create a service object
            List <string> usersThatShouldApprove = new List <string>();
            List <string> requiredRoles          = new List <string> {
                "ceo", "accountant", "cto", "hr"
            };

            var employees = new CodeMashRepository <PCEmployee>(client);
            var requests  = new CodeMashRepository <AbsenceRequest>(client);

            #endregion

            var membershipService = new CodeMashMembershipService(client);

            var allUsersList = membershipService.GetUsersList(new GetUsersRequest());

            // Employee ID
            var employeeId = requests.FindOne(x => x.Id == requestId).Employee;
            // Employee manager ID
            var employeeManagerId     = employees.FindOne(x => x.Id == employeeId).Manager;
            var employeeManagerUserId = employees.FindOne(x => x.Id == employeeManagerId).AppUserID;

            // Adding user with required roles to local users that should approve list
            foreach (var user in allUsersList.Result)
            {
                foreach (var userRole in user.Roles)
                {
                    if (requiredRoles.Contains(userRole.Name))
                    {
                        usersThatShouldApprove.Add(user.Id);
                        break;
                    }
                }
            }

            // Adding manager to users that should approve list
            foreach (var user in allUsersList.Result)
            {
                if (user.Id == employeeManagerUserId && !usersThatShouldApprove.Contains(user.Id))
                {
                    Console.WriteLine(user.FirstName + " " + user.LastName);
                    usersThatShouldApprove.Add(user.Id);
                    break;
                }
            }

            // Adding to should be approved by list
            foreach (var userId in usersThatShouldApprove)
            {
                requests.UpdateOne(x => x.Id == requestId,
                                   Builders <AbsenceRequest> .Update.AddToSet("should_be_approved_by", userId));
            }

            List <Guid> userGuids  = new List <Guid>();
            string      templateId = "02dd0ea0-79b2-4a9c-a10b-4009894958b3";

            // Convertion to guid list
            foreach (var userId in usersThatShouldApprove)
            {
                userGuids.Add(Guid.Parse(userId));
            }

            // Sending notifications to each user
            pushService.SendPushNotification(new SendPushNotificationRequest
            {
                TemplateId = Guid.Parse(templateId),
                Users      = userGuids
            }
                                             );

            return(new APIGatewayProxyResponse
            {
                Body = "lol",
                StatusCode = 200,
                Headers = new Dictionary <string, string> {
                    { "Content-Type", "application/json" }
                }
            });
        }