Beispiel #1
0
        public async Task <MessageHandlerResult> HandleAsync(AccountCreated payload, string trackingId)
        {
            ThreadContext.Properties[INTERNAL_ID] = payload.InternalId;

            try
            {
                if (Validator.IsAllowedEvent(payload))
                {
                    _logger.EventReceived(trackingId, payload);

                    if (Validator.IsValidPayload(payload))
                    {
                        AccountCreatedTransferObject user = null;
                        try
                        {
                            user = Mapper.Map <AccountCreatedTransferObject>(payload);

                            if (payload.HasSource && payload.Source == SourceSystem.STARCHEF)
                            {
                                int userId = 0;
                                if (int.TryParse(user.InternalId, out userId))
                                {
                                    user.InternalLoginId = userId;
                                }
                            }

                            var isUserExists = await DbCommands.IsUserExists(user.InternalLoginId, username : user.Username);

                            if (isUserExists)
                            {
                                /* NOTE
                                 * LoginId is missing in the system when the event is issued
                                 * #1 by another system OR
                                 * #2 be user management API (as of 18/Apr/17).
                                 * The operation to originate loginId will lookup database for the actual value.
                                 * NB: it should be originated always because User Management send event with StarChef source system.
                                 */
                                user.InternalLoginId = await DbCommands.OriginateLoginId(user.InternalLoginId, user.Username);

                                _logger.UpdatingUserExternalId(user);
                                await DbCommands.UpdateExternalId(user);
                            }
                            else
                            {
                                _logger.AddingUser(user);
                                user.InternalLoginId = await DbCommands.AddUser(user);
                            }

                            await MessagingLogger.MessageProcessedSuccessfully(payload, trackingId);

                            _logger.Processed(trackingId, payload);

                            // run subscribed post-events
                            var evt = OnProcessed;
                            if (evt != null)
                            {
                                _logger.Info("Post-processing the event");
                                await evt(this, user, _config);
                            }
                        }
                        catch (ListenerException ex)
                        {
                            _logger.ListenerException(ex, trackingId, user);
                            return(MessageHandlerResult.Fatal);
                        }
                    }
                    else
                    {
                        var errors = Validator.GetErrors();
                        _logger.InvalidModel(trackingId, payload, errors);
                        await MessagingLogger.ReceivedInvalidModel(trackingId, payload, errors);

                        return(MessageHandlerResult.Fatal);
                    }
                }
            }
            catch (System.Exception e)
            {
                _logger.Error(e);
                return(MessageHandlerResult.Fatal);
            }
            finally
            {
                ThreadContext.Properties.Remove(INTERNAL_ID);
            }
            return(MessageHandlerResult.Success);
        }