public async Task <IActionResult> AddProfile([FromBody] ProfileCreateRequest request, [FromServices] ProfileManager profileManager, [FromServices] IEmailService emailService) { await profileManager.CreateOrAttachToProfile(request, false, User.Identity.Name, new Repository <User>(_provider), emailService); return(Json(ApiResponse.Success(true))); }
public async Task CreateOrAttachToProfile(ProfileCreateRequest request, bool invitedOrganization, string userName, Repository baseRepository, IEmailService emailService = null) { var personsRep = new Repository <PersonProfile>(baseRepository); var contragentRep = new Repository <Contragent>(baseRepository); var userRepository = new Repository <User>(personsRep); var notificaitonSettingsRep = new Repository <NotificationSettings>(baseRepository); var user = await userRepository.Get(x => x.UserName == userName).SingleAsync(); var personQuery = personsRep.Get(x => x.Iin == request.PersonIin); if (!string.IsNullOrEmpty(request.OrganizationXin)) { personQuery = personQuery.Where(x => x.Contragent.Xin == request.OrganizationXin && x.Contragent.Type != ContragentTypeEnum.Individual); } var personProfile = await personQuery.FirstOrDefaultAsync(); if (personProfile == null) { personProfile = new PersonProfile() { Iin = request.PersonIin, FirstName = request.PersonFirstName, MiddleName = request.PersonSecondName, LastName = request.PersonLastName, FullName = $"{request.PersonLastName} {request.PersonFirstName} {request.PersonSecondName}".Trim(), EmployeePosition = user.DefaultProfile == null ? EmployeePositionEnum.FirstHead : EmployeePositionEnum.Employee }; ContragentTypeEnum contragentType; string contragentXin; if (!string.IsNullOrEmpty(request.OrganizationXin)) { contragentXin = request.OrganizationXin; if (request.OrganizationXin == request.PersonIin) { contragentType = ContragentTypeEnum.IndividualEnterpreneur; } else { contragentType = ContragentTypeEnum.Legal; } } else { contragentXin = request.PersonIin; contragentType = ContragentTypeEnum.Individual; } var contragent = await contragentRep.Get(x => x.Xin == contragentXin && x.Type == contragentType).SingleOrDefaultAsync(); if (contragent == null) { contragent = new Contragent() { Xin = contragentXin, FullName = !string.IsNullOrEmpty(request.OrganizationFullName) ? request.OrganizationFullName : personProfile.FullName, Type = contragentType }; contragent.UniqueId = GenerateContragentUniqueId(contragent); await contragentRep.InsertAsync(contragent); } else if (invitedOrganization) { contragent.FullName = !string.IsNullOrEmpty(request.OrganizationFullName) ? request.OrganizationFullName : personProfile.FullName; } personProfile.Contragent = contragent; await personsRep.InsertAsync(personProfile); } else { var existInCurrentUser = await userRepository.Get(x => x.Id == user.Id) .SelectMany(x => x.PersonProfiles) .Where(x => x.Id == personProfile.Id).Select(x => new { x.Id }) .SingleOrDefaultAsync(); if (existInCurrentUser != null) { throw new SmartcontractException($"Профиль с ИИН {request.PersonIin} уже зарегистрирован для текущего пользователя", ApiErrorCode.ValidationError); } } user.PersonProfiles.Add(personProfile); if (user.DefaultProfile == null) { user.DefaultProfile = personProfile; } if (emailService != null) { var bin = personProfile.Contragent.Xin; var organizationName = personProfile.Contragent.FullName; var text = $"Наименование организации:{organizationName}, БИН:{bin},"; if (personProfile.Contragent.Type == ContragentTypeEnum.Individual) { text = ""; } var notificationSettingsUser = await notificaitonSettingsRep.Get(x => x.User.Email == userName).SingleAsync(); var message = "Вы успешно добавили профиль в системе Smartcontract.kz Данные добавленного профиля:" + $"{text} ФИО:{personProfile.FullName} , ИИН: {personProfile.Iin}"; if (notificationSettingsUser.ProfileAdd) { emailService.SendEmail(userName, "Добавлен профиль на Smartcontract.kz", message); } } await userRepository.UpdateAsync(user); await personsRep.CommitAsync(); }
public async Task <ProfileCreateResponse> CreateProfileAsync(ProfileCreateRequest request) { // save hash, live and test key return(await PostAsync <ProfileCreateResponse>(ProfileCreateCommand, request)); }