Skip to content

Commit

Permalink
feat: or-1312 show adres components in search API
Browse files Browse the repository at this point in the history
  • Loading branch information
QuintenGreenstack authored and koenmetsu committed Jul 12, 2023
1 parent 10e6fd4 commit 840087e
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 47 deletions.
45 changes: 28 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace OrganisationRegistry.ElasticSearch.Projections.Organisations;
using Client;
using Osc;
using ElasticSearch.Organisations;
using Location.Events;

public static class MassUpdateOrganisationExtension
{
Expand Down Expand Up @@ -86,4 +87,50 @@ await client.TryGetAsync(
.Add("newChangeTime", changeTime)))))
.ThrowOnFailure();
}

public static async Task MassUpdateOrganisationLocationAsync(
this Elastic client,
Expression<Func<OrganisationDocument, object?>> queryFieldSelector,
object queryFieldValue,
IHasLocation updatedLocation,
int changeId,
DateTimeOffset changeTime,
int scrollSize = 100)
{
await client.TryGetAsync(
async () => (await client.WriteClient.Indices.RefreshAsync(Indices.Index<OrganisationDocument>()))
.ThrowOnFailure());

var updateByQueryResponse = (await client.WriteClient
.UpdateByQueryAsync<OrganisationDocument>(x => x
.Query(q => q
.Term(t => t
.Field(queryFieldSelector)
.Value(queryFieldValue)))
.ScrollSize(scrollSize)
.Script(s => s
.Source($"for (int i = 0; i < ctx._source.locations.size(); i++) {{" +
$"if (ctx._source.locations[i].locationId == params.idToChange) {{" +
$"ctx._source.locations[i].formattedAddress = params.formattedAddress;" +
"ctx._source.locations[i].components.street = params.street;" +
"ctx._source.locations[i].components.zipCode = params.zipCode;" +
"ctx._source.locations[i].components.municipality = params.municipality;" +
"ctx._source.locations[i].components.country = params.country;" +
"}" +
"}" +
"ctx._source.changeId = params.newChangeId;" +
"ctx._source.changeTime = params.newChangeTime;")
.Lang("painless")
.Params(p => p
.Add("idToChange", queryFieldValue)
.Add("formattedAddress", updatedLocation.FormattedAddress)
.Add("street", updatedLocation.Street)
.Add("zipCode", updatedLocation.ZipCode)
.Add("municipality", updatedLocation.City)
.Add("country", updatedLocation.Country)
.Add("newChangeId", changeId)
.Add("newChangeTime", changeTime)))));
updateByQueryResponse
.ThrowOnFailure();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace OrganisationRegistry.ElasticSearch.Projections.Organisations;
using Microsoft.Extensions.Logging;
using Common;
using Infrastructure.Change;
using Location;

public class OrganisationLocation :
BaseProjection<OrganisationLocation>,
Expand All @@ -27,9 +28,13 @@ public class OrganisationLocation :
IElasticEventHandler<OrganisationTerminated>,
IElasticEventHandler<OrganisationTerminatedV2>
{
private readonly IEventStore _store;

public OrganisationLocation(
IEventStore store,
ILogger<OrganisationLocation> logger) : base(logger)
{
_store = store;
}

public async Task<IElasticChange> Handle(
Expand All @@ -41,23 +46,20 @@ public async Task<IElasticChange> Handle(
(
elastic => elastic.TryAsync(
() => elastic
.MassUpdateOrganisationAsync(
.MassUpdateOrganisationLocationAsync(
x => x.Locations.Single().LocationId,
message.Body.LocationId,
"locations",
"locationId",
"formattedAddress",
message.Body.FormattedAddress,
message.Body,
message.Number,
message.Timestamp))
).ToAsyncResult();
}

public async Task<IElasticChange> Handle(DbConnection dbConnection, DbTransaction dbTransaction, IEnvelope<OrganisationLocationAdded> message)
=> await AddOrganisationLocation(message.Body.OrganisationId, message.Body.LocationId, message.Body.LocationFormattedAddress, message.Body.IsMainLocation, message.Body.LocationTypeId, message.Body.LocationTypeName, message.Body.ValidFrom, message.Body.ValidTo, message.Number, message.Timestamp, message.Body.OrganisationLocationId);
=> await AddOrganisationLocation(message.Body.OrganisationId, message.Body.LocationId, message.Body.LocationFormattedAddress, message.Body.IsMainLocation, message.Body.LocationTypeId, message.Body.LocationTypeName, message.Body.ValidFrom, message.Body.ValidTo, message.Number, message.Timestamp, message.Body.OrganisationLocationId, _store);

public async Task<IElasticChange> Handle(DbConnection dbConnection, DbTransaction dbTransaction, IEnvelope<KboRegisteredOfficeOrganisationLocationAdded> message)
=> await AddOrganisationLocation(message.Body.OrganisationId, message.Body.LocationId, message.Body.LocationFormattedAddress, message.Body.IsMainLocation, message.Body.LocationTypeId, message.Body.LocationTypeName, message.Body.ValidFrom, message.Body.ValidTo, message.Number, message.Timestamp, message.Body.OrganisationLocationId);
=> await AddOrganisationLocation(message.Body.OrganisationId, message.Body.LocationId, message.Body.LocationFormattedAddress, message.Body.IsMainLocation, message.Body.LocationTypeId, message.Body.LocationTypeName, message.Body.ValidFrom, message.Body.ValidTo, message.Number, message.Timestamp, message.Body.OrganisationLocationId, _store);

public async Task<IElasticChange> Handle(DbConnection dbConnection, DbTransaction dbTransaction, IEnvelope<KboRegisteredOfficeOrganisationLocationRemoved> message)
=> await RemoveOrganisationLocation(message.Body.OrganisationId, message.Number, message.Timestamp, message.Body.OrganisationLocationId);
Expand Down Expand Up @@ -121,9 +123,10 @@ public async Task<IElasticChange> Handle(DbConnection dbConnection, DbTransactio
message.Body.LocationTypeId,
message.Body.LocationTypeName,
message.Body.ValidFrom,
message.Body.ValidTo);
message.Body.ValidTo,
_store);

private static async Task<IElasticChange> AddOrganisationLocation(Guid organisationId, Guid locationId, string locationFormattedAddress, bool isMainLocation, Guid? locationTypeId, string? locationTypeName, DateTime? validFrom, DateTime? validTo, int documentChangeId, DateTimeOffset timestamp, Guid organisationLocationId)
private static async Task<IElasticChange> AddOrganisationLocation(Guid organisationId, Guid locationId, string locationFormattedAddress, bool isMainLocation, Guid? locationTypeId, string? locationTypeName, DateTime? validFrom, DateTime? validTo, int documentChangeId, DateTimeOffset timestamp, Guid organisationLocationId, IEventStore store)
{
return await new ElasticPerDocumentChange<OrganisationDocument>(
organisationId,
Expand All @@ -132,6 +135,13 @@ private static async Task<IElasticChange> AddOrganisationLocation(Guid organisat
document.ChangeId = documentChangeId;
document.ChangeTime = timestamp;
var eventEnvelopes = store.GetEventEnvelopes<Location>(locationId);
var lastEvent = (IHasLocation)eventEnvelopes
.OrderBy(x => x.Number)
.Last()
.Body;
document.Locations.RemoveExistingListItems(
x =>
x.OrganisationLocationId == organisationLocationId);
Expand All @@ -141,6 +151,11 @@ private static async Task<IElasticChange> AddOrganisationLocation(Guid organisat
organisationLocationId,
locationId,
locationFormattedAddress,
new OrganisationDocument.LocationComponents(
lastEvent.Street,
lastEvent.ZipCode,
lastEvent.City,
lastEvent.Country),
isMainLocation,
locationTypeId,
locationTypeName,
Expand All @@ -150,38 +165,51 @@ private static async Task<IElasticChange> AddOrganisationLocation(Guid organisat
}

private static async Task<IElasticChange> UpdateOrganisationLocation(
Guid bodyOrganisationId,
Guid organisationId,
int organisationDocumentChangeId,
DateTimeOffset organisationDocumentChangeTime,
Guid bodyOrganisationLocationId,
Guid bodyLocationId,
string bodyLocationFormattedAddress,
bool bodyIsMainLocation,
Guid? bodyLocationTypeId,
string? bodyLocationTypeName,
DateTime? bodyValidFrom,
DateTime? bodyValidTo)
Guid organisationLocationId,
Guid locationId,
string locationFormattedAddress,
bool isMainLocation,
Guid? locationTypeId,
string? locationTypeName,
DateTime? validFrom,
DateTime? validTo,
IEventStore store)
{
return await new ElasticPerDocumentChange<OrganisationDocument>(
bodyOrganisationId,
organisationId,
document =>
{
document.ChangeId = organisationDocumentChangeId;
document.ChangeTime = organisationDocumentChangeTime;
document.Locations.RemoveExistingListItems(
x =>
x.OrganisationLocationId == bodyOrganisationLocationId);
x.OrganisationLocationId == organisationLocationId);
var eventEnvelopes = store.GetEventEnvelopes<Location>(locationId);
var lastEvent = (IHasLocation)eventEnvelopes
.OrderBy(x => x.Number)
.Last()
.Body;
document.Locations.Add(
new OrganisationDocument.OrganisationLocation(
bodyOrganisationLocationId,
bodyLocationId,
bodyLocationFormattedAddress,
bodyIsMainLocation,
bodyLocationTypeId,
bodyLocationTypeName,
Period.FromDates(bodyValidFrom, bodyValidTo)));
organisationLocationId,
locationId,
locationFormattedAddress,
new OrganisationDocument.LocationComponents(
lastEvent.Street,
lastEvent.ZipCode,
lastEvent.City,
lastEvent.Country),
isMainLocation,
locationTypeId,
locationTypeName,
Period.FromDates(validFrom, validTo)));
}
).ToAsyncResult();
}
Expand Down
Loading

0 comments on commit 840087e

Please sign in to comment.