예제 #1
0
        public OperationResult AddPrize(long passId, long clientId, bool sendMail, DateTime?expiryDate, long?managerId)
        {
            return(InvokeOperations.InvokeOperation(() =>
            {
                Logger.Trace("Add prize (pass id {1}) for client {0}", clientId, passId);

                var newPassId = tournamentRepository.AddTournamentClientPass(clientId, passId, expiryDate);
                var resPrize = clientService.AddPrize(clientId, newPassId, (short)PrizeType.Tournament, expiryDate, managerId);
                if (!resPrize.IsSuccess)
                {
                    throw new OperationException(resPrize.Error, resPrize.Code);
                }



                if (sendMail)
                {
                    var client = clientService.GetClient(clientId);
                    if (!client.IsSuccess)
                    {
                        return;
                    }

                    var pass = tournamentRepository.GetTournamentPass(passId);
                    if (pass == null)
                    {
                        return;
                    }
                    var tournament = tournamentRepository.GetTournament(pass.tournament_id);
                    if (tournament == null)
                    {
                        return;
                    }

                    var template = contentService.GetMessageTemplate(type, LangHelper.GetLang(client.Result.Country));
                    template.Result = template.Result ?? new MessageTemplate {
                        Template = ""
                    };

                    var body = MessageTemplateHelper.ReplaceKeys(template.Result.Template, template.Result.Template,
                                                                 new[] { KeyName, KeyTournament },
                                                                 new[] { string.Format("{0} {1}", client.Result.FirstName, client.Result.LastName), tournament.name });

                    if (!string.IsNullOrEmpty(body))
                    {
                        Task.Factory.StartNew(() => mailingService.SendMail(client.Result.Email, "Tournament free pass", body, body));
                    }
                }
            }));
        }