Exemple #1
0
        public async Task <IActionResult> SetupOrganization(SetupOrganizationDto setupOrganizationDto)
        {
            //#MISSING: ADD ORGANIZATIONID TO ORGANIZATION DEPENDING ON USER LOGGED
            //ASSIGN DATA AND THEN VALIDATE THE WHOLE OBJECT
            try
            {
                string message;
                bool   organizationExists = await _organizationService.ExistsOrganization(setupOrganizationDto.Organization.Name);

                if (!organizationExists)
                {
                    var address   = _mapper.Map <Address>(setupOrganizationDto.Address);
                    var addressId = await _addressService.InsertAddress(address);

                    var organization = _mapper.Map <Organization>(setupOrganizationDto.Organization);
                    organization.AddressId = addressId;
                    var organizationId = await _organizationService.InsertOrganization(organization);

                    var facilities = _mapper.Map <List <Facility> >(setupOrganizationDto.Facilities);
                    facilities.ForEach(e => e.OrganizationId = organizationId);
                    var facilitiesIdDic = await _facilityService.InsertFacilitiesRange(facilities);

                    var invites = _mapper.Map <List <Invite> >(setupOrganizationDto.Users);
                    invites.ForEach(e => e.OrganizationId = organizationId);
                    await _inviteService.InsertInvitesRange(invites);

                    setupOrganizationDto.Units.ForEach(e => e.FacilityId = facilitiesIdDic[e.Facility]);
                    var units = _mapper.Map <List <Unit> >(setupOrganizationDto.Units);
                    await _unitService.InsertUnitsRange(units);

                    message = "The organization has been registered.";
                    var response = new DeviceApiResponse <bool>(message);
                    return(Ok(response));
                }
                else
                {
                    message = "The Organization name already exists in the Organization.";
                    var response = new DeviceApiResponse <bool>(message);
                    return(BadRequest(response));
                }
            }
            catch (Exception ex)
            {
                string message  = "ERROR";
                var    response = new ApiResponse <Exception>(ex, message);
                return(StatusCode(500, response));
            }
        }