Beispiel #1
0
 public async Task <IActionResult> FetchCompanyVenues([FromRoute(Name = "companyId")] int companyId, [FromQuery(Name = "page")] int page = 0, [FromQuery(Name = "count")] int count = 20)
 {
     return(await venues.FetchCompanyVenues(companyId, page, count)
            .Ensure(v => v.HasValue, "Venues were found")
            .OnBoth(v => v.IsFailure ? StatusCode(404, "") : StatusCode(200, v.Value.Value))
            .ConfigureAwait(false));
 }
Beispiel #2
0
        private async Task SynchVenuesWithExternalPaymentProvider(string accessToken, Company company)
        {
            if (company == null)
            {
                return;
            }

            try
            {
                var client = new SquareClient.Builder()
                             .Environment(_settings.Connection.SquarePaymentsSandbox ? Square.Environment.Sandbox : Square.Environment.Production)
                             .AccessToken(accessToken)
                             .Build();

                ListLocationsResponse result = await client.LocationsApi.ListLocationsAsync().ConfigureAwait(false);

                var companyVenues = await _venues.FetchCompanyVenues(company.CompanyId, 0, int.MaxValue)
                                    .OnSuccess(c => c.Value.ToList())
                                    .ConfigureAwait(true);

                if (companyVenues.IsSuccess)
                {
                    foreach (var item in companyVenues.Value)
                    {
                        try
                        {
                            var matchedLocation = result.Locations
                                                  .FirstOrDefault(
                                x => NormaliseString(x.Name) == NormaliseString(item.VenueName) &&
                                NormaliseString(x.Address.PostalCode) == NormaliseString(item.VenuePostCode));

                            // need to find a match in Square location
                            if (matchedLocation != null)
                            {
                                if (string.IsNullOrWhiteSpace(item.ExternalLocationId) && matchedLocation != null)
                                {
                                    // We have a match and need to update local Database
                                    await _venues.UpdateVenue(new VenuePatch
                                    {
                                        ResourceId         = item.VenueId,
                                        ExternalLocationId = new PatchOperation <string> {
                                            Operation = OperationKind.Update, Value = matchedLocation.Id
                                        },
                                    }).ConfigureAwait(false);
                                }
                            }
                            else if (matchedLocation == null)
                            {
                                // we have a location in our system that does not exist in sqaure. Need to add one
                                var createLocation = new CreateLocationRequest(new Location(
                                                                                   name: item.VenueName,
                                                                                   address:
                                                                                   new Address(
                                                                                       addressLine1: string.IsNullOrWhiteSpace(item.VenueAddress) ? "Not supplied" : item.VenueAddress,
                                                                                       addressLine2: item.VenueAddress2,
                                                                                       addressLine3: item.VenueAddress3,
                                                                                       postalCode: string.IsNullOrWhiteSpace(item.VenuePostCode) ? "Not supplied" : item.VenuePostCode,
                                                                                       locality: string.IsNullOrWhiteSpace(item.VenueCounty) ? "Not supplied" : item.VenueCounty,
                                                                                       firstName: item.VenueContact),
                                                                                   status: "ACTIVE",
                                                                                   phoneNumber: GetVenuePhoneNumber(company, item)));

                                var newLocation = client.LocationsApi.CreateLocation(createLocation);

                                if (newLocation.Location != null)
                                {
                                    await _venues.UpdateVenue(new VenuePatch
                                    {
                                        ResourceId         = item.VenueId,
                                        ExternalLocationId = new PatchOperation <string> {
                                            Operation = OperationKind.Update, Value = newLocation.Location.Id
                                        },
                                    }).ConfigureAwait(false);
                                }
                            }
                        }
                        catch (ApiException e)
                        {
                            Logger.LogError(e, $"Failed to create business location for venue {item.VenueName}");
                        }
                    }
                }
            }
            catch (ApiException e)
            {
                Logger.LogError(e, $"Failed to connect to Square API");
            }
        }