Ejemplo n.º 1
0
        /// <summary>
        /// Ici on va traiter la demande que l'on a sauvegardée
        /// on récupère les infos
        /// on crée une réponse
        /// et on envoie la réponse à AssurBox via l'api
        /// </summary>
        /// <param name="key"></param>
        public void ExecuteJob(string key)
        {
            Logger.Log($"ContractTermination.ExecuteJob - Debut {key}");

            // recherche dans la table
            SDK.WebHooks.AssurBoxWebHook assurBoxNotification;
            using (DAL.ApiDataContext ctx = new DAL.ApiDataContext())
            {
                var request = ctx.CarGreenCardRequests.AsNoTracking().FirstOrDefault(x => x.RequestId == key);
                assurBoxNotification = JsonConvert.DeserializeObject <SDK.WebHooks.AssurBoxWebHook>(request.RawRequest);
            }


            // 1. retrieve a token to access the AssurBox Api
            var tokenInfo = AssurBoxSecurity.GetAssurBoxSecurityToken();

            // 2. use the client
            SDK.Clients.ContractTerminationClient client = new SDK.Clients.ContractTerminationClient(new AssurBoxClientOptions {
                Host = Config.AssurBoxApiBaseURL, ApiKey = tokenInfo
            });

            // 3. using the authentified client, retrieve the details of the  request
            //    full detail about the customer, car information, ...
            //var requestDetails = client.(assurBoxNotification.CorrelationId).Result;

            SDK.DTO.Contract.ContractTerminationInfo requestinfo = client.GetContractTerminationRequestAsync(assurBoxNotification.CorrelationId).Result;

            // 4. do something with the request in your information system

            // ...

            // 5. send a response to the requester
            string acceptation = GetAcceptation(requestinfo.Request.Risk);
            var    resp        = client.SendContractTerminationResponseAsync(new SDK.DTO.Contract.ContractTerminationResponse
            {
                CorrelationId = assurBoxNotification.CorrelationId,
                MessageId     = assurBoxNotification.MessageId,
                Communication = acceptation,
                IsTerminationRequestAccepted = acceptation == "OK",
                EffectiveTerminationDate     = DateTime.Today.AddDays(7)
            }).Result;

            // todo change "green card"
            using (DAL.ApiDataContext ctx = new DAL.ApiDataContext())
            {
                var request = ctx.CarGreenCardRequests.FirstOrDefault(x => x.RequestId == key);
                request.ResponseInfo       = "Response sent  " + resp.ResponseContent;
                request.RequestRespondDate = DateTime.UtcNow;
                ctx.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handle a notification for a green card request
        /// </summary>
        /// <param name="key"></param>
        /// <param name="assurBoxNotification"></param>
        private void ProcessModificationRequest(string key, GreenCardRequestNotification assurBoxNotification)
        {
            // 1. retrieve a token to access the AssurBox Api
            var tokenInfo = AssurBoxSecurity.GetAssurBoxSecurityToken();

            // 2. using the token, create a "greencard client"
            CarGreenCardClient client = new CarGreenCardClient(new AssurBoxClientOptions {
                Host = Config.AssurBoxApiBaseURL, ApiKey = tokenInfo
            });

            // 3. using the authentified client, retrieve the details of the green card request
            //    full detail about the customer, car information, ...
            var requestDetails = client.GetRequest(assurBoxNotification.CorrelationId).Result;

            // 4. do something with the request in your information system
            var document = GetDemoPDFUpdate(requestDetails);

            // 5. send a response to the requester, using the greencard client and a greencardrequestresponse object
            GreenCardRequestResponse response = new GreenCardRequestResponse();

            // The correlation id identify a greencard request file (this is mandatory)
            response.CorrelationId = assurBoxNotification.CorrelationId;
            // The messageid identify a specific message (this is mandatory)
            response.MessageId = assurBoxNotification.MessageId;

            string validationMessage;

            if (ValidateRequest(assurBoxNotification, out validationMessage) == false)
            {
                // We can send a response refusing to issue a green card
                response.HasApproval     = false;
                response.ApprovalReason  = validationMessage;
                response.ResponseContent = validationMessage;
            }
            else
            {
                response.HasApproval = true; // don't forget to set this property to true

                // define a message for the requester
                response.ResponseContent = $@"
                    Bonjour {assurBoxNotification.Requester.Name},

                    Merci pour votre demande, (type : {assurBoxNotification.NotificationType})

                    Voici votre carte mise à jour pour {assurBoxNotification.LicencePlate}

                    Client : {requestDetails.CustomerName}

                    Bien à vous,
                    Assurance simulation demo ({requestDetails.InsuranceName})
                    ";

                // make sure the file is encoded as a base64 string
                response.AddAttachment($"CarteVerte_{assurBoxNotification.LicencePlate}.pdf", Convert.ToBase64String(document));
            }

            // send the response to AssurBox
            var resp = client.SendResponse(response).Result;

            using (DAL.ApiDataContext ctx = new DAL.ApiDataContext())
            {
                var request = ctx.CarGreenCardRequests.FirstOrDefault(x => x.RequestId == key);
                request.ResponseInfo       = "Updated response  " + resp.ResponseContent;
                request.RequestRespondDate = DateTime.UtcNow;
                ctx.SaveChanges();
            }
        }