From d9717033cf3d64ded19619f429afa497de4421e3 Mon Sep 17 00:00:00 2001 From: MahanCh Date: Sat, 2 Aug 2025 14:27:55 +0330 Subject: [PATCH] add Create method in institutionContractController to handle institution contract creation with validations and account registration --- .../institutionContractController.cs | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/ServiceHost/Areas/Admin/Controllers/institutionContractController.cs b/ServiceHost/Areas/Admin/Controllers/institutionContractController.cs index 035a8db2..57c0abc5 100644 --- a/ServiceHost/Areas/Admin/Controllers/institutionContractController.cs +++ b/ServiceHost/Areas/Admin/Controllers/institutionContractController.cs @@ -1,5 +1,8 @@ using _0_Framework.Application; +using AccountManagement.Application.Contracts.Account; using CompanyManagment.App.Contracts.InstitutionContract; +using CompanyManagment.App.Contracts.InstitutionContractContactinfo; +using CompanyManagment.App.Contracts.PersonalContractingParty; using Microsoft.AspNetCore.Mvc; using ServiceHost.BaseControllers; @@ -11,10 +14,17 @@ namespace ServiceHost.Areas.Admin.Controllers; public class institutionContractController : AdminBaseController { private readonly IInstitutionContractApplication _institutionContractApplication; + private readonly IPersonalContractingPartyApp _contractingPartyApplication; + private readonly IContactInfoApplication _contactInfoApplication; + private readonly IAccountApplication _accountApplication; + - public institutionContractController(IInstitutionContractApplication institutionContractApplication) + public institutionContractController(IInstitutionContractApplication institutionContractApplication, IPersonalContractingPartyApp contractingPartyApplication, IContactInfoApplication contactInfoApplication, IAccountApplication accountApplication) { - _institutionContractApplication = institutionContractApplication; + _institutionContractApplication = institutionContractApplication; + _contractingPartyApplication = contractingPartyApplication; + _contactInfoApplication = contactInfoApplication; + _accountApplication = accountApplication; } /// @@ -26,4 +36,64 @@ public class institutionContractController : AdminBaseController { return await _institutionContractApplication.GetList(searchModel); } + + [HttpPost] + public async Task>Create([FromBody]CreateInstitutionContract command) + { + var op = new OperationResult(); + var counter = command.ContactInformationList.Count; + //if (string.IsNullOrWhiteSpace(command.HasValueAddedTax)) + // command.HasValueAddedTax = "false"; + var phone = command.ContactInformationList.FirstOrDefault(x => + x.SendSmsString == "true" && x.Position == "طرف قرارداد" && x.PhoneType == "شماره همراه"); + var conractingParty = _contractingPartyApplication.GetDetails(command.ContractingPartyId); + if (conractingParty.IsLegal == "حقیقی" && string.IsNullOrWhiteSpace(conractingParty.Nationalcode)) + return new JsonResult(op.Failed("کد ملی طرف حساب وجود ندارد")); + if (conractingParty.IsLegal == "حقوقی" && string.IsNullOrWhiteSpace(conractingParty.NationalId)) + return new JsonResult(op.Failed("شناسه ملی طرف حساب وجود ندارد")); + var result = _institutionContractApplication.Create(command); + + if (result.IsSuccedded && counter > 0) + { + for (var i = 0; i <= counter - 1; i++) + { + if (command.ContactInformationList[i].PhoneNumber != null) + { + var contactinfo = new CreateContactInfo + { + InstitutionContractId = result.SendId, + PhoneType = command.ContactInformationList[i].PhoneType, + Position = command.ContactInformationList[i].Position, + PhoneNumber = command.ContactInformationList[i].PhoneNumber, + FnameLname = command.ContactInformationList[i].FnameLname, + SendSms = command.ContactInformationList[i].SendSmsString == "true" ? true : false + }; + _contactInfoApplication.Create(contactinfo); + } + + Thread.Sleep(500); + } + + + if (phone != null) + { + var userPass = conractingParty.IsLegal == "حقیقی" + ? conractingParty.Nationalcode + : conractingParty.NationalId; + var createAcc = new RegisterAccount + { + Fullname = conractingParty.LName, + Username = userPass, + Password = userPass, + Mobile = phone.PhoneNumber, + NationalCode = userPass + }; + var res = _accountApplication.RegisterClient(createAcc); + if (res.IsSuccedded) + _institutionContractApplication.CreateContractingPartyAccount(command.ContractingPartyId, res.SendId); + } + } + + return new JsonResult(result); + } } \ No newline at end of file