Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: or-1312 show adres components in search API #1086

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading