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

Create patient resource using a deterministic id #183

Merged
merged 7 commits into from
Apr 15, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using EnsureThat;
using Hl7.Fhir.Model;
Expand Down Expand Up @@ -69,13 +71,36 @@ protected async Task<TResource> CreateResourceByIdentityAsync<TResource>(Model.I

propertySetter?.Invoke(resource, identifier);

return await FhirService.CreateResourceAsync(resource).ConfigureAwait(false);
// Generate id programatically and issue an update to FHIR service
// The resource will be created if the resource with the given id does not already exist.
resource.Id = ComputeSha256(identifier);
return await FhirService.UpdateResourceAsync<TResource>(resource).ConfigureAwait(false);
}

private static Model.Identifier BuildIdentifier(string value, string system)
{
var identifier = new Model.Identifier { Value = value, System = string.IsNullOrWhiteSpace(system) ? null : system };
return identifier;
}

private static string ComputeSha256(Model.Identifier identifier)
{
EnsureArg.IsNotNullOrWhiteSpace(identifier.Value, nameof(identifier.Value));

string plainTextSystemAndId = $"{identifier.System}_{identifier.Value}";

using (SHA256 hashAlgorithm = SHA256.Create())
dustinburson marked this conversation as resolved.
Show resolved Hide resolved
{
byte[] bytes = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(plainTextSystemAndId));

StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
sb.Append(bytes[i].ToString("x2"));
}

return sb.ToString();
}
}
}
}