Ejemplo n.º 1
0
        public async Task <FunctionContainerResponse> Handle(SaasafrasSolutionCommand <SaasafrasPodioEvent> command)
        {
            client.log("This will go the client log file.");
            solution.log("This will go to the solution log file.");

            //check for valid itemId
            if (!int.TryParse(command.resource.item_id, out int itemId))
            {
                throw new ArgumentException("we were expecting an integer");
            }

            //get Podio client
            var podio = new Podio(accessTokenProvider);


            var userService = new PodioCore.Services.UserService(podio);

            solution.log("calling podio");
            var user = await userService.GetUser();

            solution.log($"podio api user_id : {user.UserId}");
            solution.log($"podio api email : {user.Mail}");

            //get item that fired the event
            var currentItem = await podio.GetFullItem(itemId) ?? throw new Exception($"failed to get item {itemId}");

            client.log($"Item was created in {currentItem.App.Config.Name}.");

            //update the function name
            var functionName = "adamHelloWorldFlow";
            var uniqueId     = currentItem.ItemId.ToString();
            var lockId       = await functionLocker.LockFunction(functionName, uniqueId);

            if (string.IsNullOrEmpty(lockId))
            {
                throw new Exception($"Failed to acquire lock for {functionName} and id {uniqueId}");
            }

            try
            {
                /**=======================
                 * WRITE YOU LOGIC HERE !
                 * =========================*/


                // easily reference Podio spaces, apps and fields
                //var mySolution = await saasafrasDictionary.GetDictionary();
                //int MyAppMyFieldId = int.Parse(dictionary["My Space| My App | My Field"])



                // access spaces, apps or fields from the solution dictionary
                //var dictionary = await saasafrasDictionary.GetDictionary();

                //var demoApp = dictionary["!BBC - Playground Test Space| Demo App"];
                //Console.WriteLine(demoApp);



                //int FieldsAppTitleFieldId = int.Parse(dictionary["!BBC - Playground Test Space|Field Types|Title"]);

                //System.Console.WriteLine(FieldsAppTitleFieldId); // id of the field

                //var FieldsAppTitleField = currentItem.Field<TextItemField>(FieldsAppTitleFieldId);

                //System.Console.WriteLine(FieldsAppTitleField.Label); // id of the field

                await podio.CommentOnItem("adams first comment", currentItem.ItemId, false);
            }

            catch (Exception ex)
            {
                Console.WriteLine($"Function {functionName}");
                Console.WriteLine($"Exception: {ex}");
            }

            finally
            {
                await functionLocker.UnlockFunction(functionName, uniqueId, lockId);
            }

            return(new FunctionContainerResponse
            {
                message = "success"
            });
        }