Skip to content

Commit

Permalink
Merge pull request #1684 from ministryofjustice/MLPAB-2018-send-uid-f…
Browse files Browse the repository at this point in the history
…or-correspondent

MLPAB-2018 Add a UID to correspondent and send with event
  • Loading branch information
hawx authored Dec 13, 2024
2 parents 67d71c6 + 2fffe51 commit 7e754c2
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 101 deletions.
6 changes: 4 additions & 2 deletions cypress/e2e/donor/add-correspondent.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ describe('Add correspondent', () => {
.then((uid) => {
cy.visit(`http://localhost:9001/?detail-type=correspondent-updated&detail=${uid}`);

cy.contains(`{"uid":"${uid}","firstNames":"John","lastName":"Smith","email":"email@example.com"}`);
cy.contains(`{"uid":"${uid}",`);
cy.contains(`"firstNames":"John","lastName":"Smith","email":"email@example.com"}`);
});
});

Expand Down Expand Up @@ -69,7 +70,8 @@ describe('Add correspondent', () => {
.then((uid) => {
cy.visit(`http://localhost:9001/?detail-type=correspondent-updated&detail=${uid}`);

cy.contains(`{"uid":"${uid}","firstNames":"John","lastName":"Smith","email":"email@example.com","address":{"line1":"2 RICHMOND PLACE","line2":"","line3":"","town":"BIRMINGHAM","postcode":"B14 7ED","country":"GB"}}`);
cy.contains(`{"uid":"${uid}",`);
cy.contains(`"firstNames":"John","lastName":"Smith","email":"email@example.com","address":{"line1":"2 RICHMOND PLACE","line2":"","line3":"","town":"BIRMINGHAM","postcode":"B14 7ED","country":"GB"}}`);
});
});
});
159 changes: 76 additions & 83 deletions docker/mock-lpa-store/lpa-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,99 @@ const lpaStore = stores.open('lpa');
const pathParts = context.request.path.split('/');
const lpaUID = pathParts[2]

switch (context.request.method) {
case 'GET': {
if (pathParts.length == 3 && pathParts[1] == 'lpas') {
const lpa = lpaStore.load(lpaUID);
if (lpa) {
respond().withContent(lpa);
} else {
respond().withStatusCode(404);
}
if (context.request.method == 'GET') {
if (pathParts.length == 3 && pathParts[1] == 'lpas') {
const lpa = lpaStore.load(lpaUID);
if (lpa) {
respond().withContent(lpa);
} else {
respond();
respond().withStatusCode(404);
}
break;
}
case 'PUT': {
let lpa = JSON.parse(context.request.body);
lpa.uid = lpaUID;
lpa.updatedAt = new Date(Date.now()).toISOString();
lpaStore.save(lpaUID, JSON.stringify(lpa));
} else {
respond();
break;
}
case 'POST': {
if (context.request.path == '/lpas') {
let uids = JSON.parse(context.request.body).uids;
let lpas = uids.map(uid => lpaStore.load(uid)).reduce((list, lpa) => lpa ? list.concat([JSON.parse(lpa)]) : list, []);
} else if (context.request.method == 'PUT') {
let lpa = JSON.parse(context.request.body);
lpa.uid = lpaUID;
lpa.updatedAt = new Date(Date.now()).toISOString();
lpaStore.save(lpaUID, JSON.stringify(lpa));
respond();
} else if (context.request.method == 'POST') {
if (context.request.path == '/lpas') {
let uids = JSON.parse(context.request.body).uids;
let lpas = uids.map(uid => lpaStore.load(uid)).reduce((list, lpa) => lpa ? list.concat([JSON.parse(lpa)]) : list, []);

respond().withContent(JSON.stringify({ lpas: lpas }));
} else {
let update = JSON.parse(context.request.body);
let lpa = JSON.parse(lpaStore.load(lpaUID));
if (!lpa) {
respond().withStatusCode(404);
break;
}
lpa.updatedAt = new Date(Date.now()).toISOString();
respond().withContent(JSON.stringify({ lpas: lpas }));
} else {
let update = JSON.parse(context.request.body);
let lpa = JSON.parse(lpaStore.load(lpaUID));
if (!lpa) {
respond().withStatusCode(404);
return void 0;
}
lpa.updatedAt = new Date(Date.now()).toISOString();

switch (update.type) {
case 'ATTORNEY_SIGN': {
const keyParts = update.changes[0].key.split('/');
const idx = parseInt(keyParts[2]);
const signedAt = update.changes.find(x => x.key.includes('signedAt')).new;
switch (update.type) {
case 'ATTORNEY_SIGN': {
const keyParts = update.changes[0].key.split('/');
const idx = parseInt(keyParts[2]);
const signedAt = update.changes.find(x => x.key.includes('signedAt')).new;

if (lpa.attorneys && idx < lpa.attorneys.length) {
lpa.attorneys[idx].signedAt = signedAt;
}
break;
if (lpa.attorneys && idx < lpa.attorneys.length) {
lpa.attorneys[idx].signedAt = signedAt;
}
case 'TRUST_CORPORATION_SIGN': {
const keyParts = update.changes[0].key.split('/');
const idx = parseInt(keyParts[2]);
const signedAt = update.changes.find(x => x.key.includes('signedAt')).new;
break;
}
case 'TRUST_CORPORATION_SIGN': {
const keyParts = update.changes[0].key.split('/');
const idx = parseInt(keyParts[2]);
const signedAt = update.changes.find(x => x.key.includes('signedAt')).new;

if (lpa.trustCorporations && idx < lpa.trustCorporations.length) {
lpa.trustCorporations[idx].signatories = [{ firstNames: "A", lastName: "Sign", signedAt: signedAt }];
}
break;
if (lpa.trustCorporations && idx < lpa.trustCorporations.length) {
lpa.trustCorporations[idx].signatories = [{ firstNames: "A", lastName: "Sign", signedAt: signedAt }];
}
case 'CERTIFICATE_PROVIDER_SIGN':
const signedAt = update.changes.find(x => x.key.includes('signedAt')).new;
lpa.certificateProvider.signedAt = signedAt;
break;

case 'STATUTORY_WAITING_PERIOD':
lpa.status = 'statutory-waiting-period';
break;
break;
}
case 'CERTIFICATE_PROVIDER_SIGN':
const signedAt = update.changes.find(x => x.key.includes('signedAt')).new;
lpa.certificateProvider.signedAt = signedAt;
break;

case 'REGISTER':
if (lpa.status === 'statutory-waiting-period') {
lpa.status = 'registered';
lpa.registrationDate = new Date(Date.now()).toISOString();
}
break;
case 'STATUTORY_WAITING_PERIOD':
lpa.status = 'statutory-waiting-period';
break;

case 'CERTIFICATE_PROVIDER_OPT_OUT':
lpa.status = 'cannot-register';
break;
case 'REGISTER':
if (lpa.status == 'statutory-waiting-period') {
lpa.status = 'registered';
lpa.registrationDate = new Date(Date.now()).toISOString();
}
break;

case 'DONOR_WITHDRAW_LPA':
lpa.status = 'withdrawn';
break;
case 'CERTIFICATE_PROVIDER_OPT_OUT':
lpa.status = 'cannot-register';
break;

case 'ATTORNEY_OPT_OUT':
const idx = lpa.attorneys.findIndex(item => item.uid === update.subject)
case 'DONOR_WITHDRAW_LPA':
lpa.status = 'withdrawn';
break;

if (idx >= 0 && lpa.attorneys[idx].signedAt != '') {
lpa.attorneys[idx].status = 'removed'
}
break;
case 'ATTORNEY_OPT_OUT':
const idx = lpa.attorneys.findIndex(item => item.uid == update.subject)

case 'OPG_STATUS_CHANGE':
lpa.status = update.changes.find(x => x.key == '/status').new;
break;
}
if (idx >= 0 && lpa.attorneys[idx].signedAt != '') {
lpa.attorneys[idx].status = 'removed'
}
break;

lpaStore.save(lpaUID, JSON.stringify(lpa));
respond();
case 'OPG_STATUS_CHANGE':
lpa.status = update.changes.find(x => x.key == '/status').new;
break;
}
break;
}
default:

lpaStore.save(lpaUID, JSON.stringify(lpa));
respond();
}
} else {
respond();
}
2 changes: 2 additions & 0 deletions internal/donor/donordata/correspondent.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package donordata

import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
)

type Correspondent struct {
UID actoruid.UID
FirstNames string
LastName string
Email string
Expand Down
8 changes: 4 additions & 4 deletions internal/donor/donordata/provided_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func TestGenerateHash(t *testing.T) {
}

// DO change this value to match the updates
const modified uint64 = 0x42eaf05fa4af6121
const modified uint64 = 0x2556115d1e0785cd

// DO NOT change these initial hash values. If a field has been added/removed
// you will need to handle the version gracefully by modifying
// (*Provided).HashInclude and adding another testcase for the new
// version.
testcases := map[uint8]uint64{
0: 0xe929f7d694b60743,
0: 0xf5086332ffe66299,
}

for version, initial := range testcases {
Expand Down Expand Up @@ -99,13 +99,13 @@ func TestGenerateCheckedHash(t *testing.T) {
}

// DO change this value to match the updates
const modified uint64 = 0x51d901b0a335d248
const modified uint64 = 0x70d44213a2b8cce3

// DO NOT change these initial hash values. If a field has been added/removed
// you will need to handle the version gracefully by modifying
// toCheck.HashInclude and adding another testcase for the new version.
testcases := map[uint8]uint64{
0: 0x8681da14cea4a7f4,
0: 0xa9fccd755b8d87b7,
}

for version, initial := range testcases {
Expand Down
1 change: 1 addition & 0 deletions internal/donor/donorpage/enter_correspondent_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func EnterCorrespondentAddress(logger Logger, tmpl template.Template, addressCli

if err := eventClient.SendCorrespondentUpdated(r.Context(), event.CorrespondentUpdated{
UID: provided.LpaUID,
ActorUID: &provided.Correspondent.UID,
FirstNames: provided.Correspondent.FirstNames,
LastName: provided.Correspondent.LastName,
Email: provided.Correspondent.Email,
Expand Down
12 changes: 10 additions & 2 deletions internal/donor/donorpage/enter_correspondent_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
Expand Down Expand Up @@ -141,12 +142,15 @@ func TestPostEnterCorrespondentAddressManual(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(f.Encode()))
r.Header.Add("Content-Type", page.FormUrlEncoded)

actorUID := actoruid.New()

donorStore := newMockDonorStore(t)
donorStore.EXPECT().
Put(r.Context(), &donordata.Provided{
LpaID: "lpa-id",
LpaUID: "lpa-uid",
Correspondent: donordata.Correspondent{
UID: actorUID,
Address: testAddress,
},
Tasks: donordata.Tasks{
Expand All @@ -158,14 +162,18 @@ func TestPostEnterCorrespondentAddressManual(t *testing.T) {
eventClient := newMockEventClient(t)
eventClient.EXPECT().
SendCorrespondentUpdated(r.Context(), event.CorrespondentUpdated{
UID: "lpa-uid",
Address: &testAddress,
UID: "lpa-uid",
ActorUID: &actorUID,
Address: &testAddress,
}).
Return(nil)

err := EnterCorrespondentAddress(nil, nil, nil, donorStore, eventClient)(testAppData, w, r, &donordata.Provided{
LpaID: "lpa-id",
LpaUID: "lpa-uid",
Correspondent: donordata.Correspondent{
UID: actorUID,
},
})
resp := w.Result()

Expand Down
8 changes: 7 additions & 1 deletion internal/donor/donorpage/enter_correspondent_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata"
Expand All @@ -23,7 +24,7 @@ type enterCorrespondentDetailsData struct {
NameWarning *actor.SameNameWarning
}

func EnterCorrespondentDetails(tmpl template.Template, donorStore DonorStore, eventClient EventClient) Handler {
func EnterCorrespondentDetails(tmpl template.Template, donorStore DonorStore, eventClient EventClient, newUID func() actoruid.UID) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *donordata.Provided) error {
data := &enterCorrespondentDetailsData{
App: appData,
Expand All @@ -42,6 +43,10 @@ func EnterCorrespondentDetails(tmpl template.Template, donorStore DonorStore, ev
data.Errors = data.Form.Validate()

if data.Errors.None() {
if provided.Correspondent.UID.IsZero() {
provided.Correspondent.UID = newUID()
}

provided.Correspondent.FirstNames = data.Form.FirstNames
provided.Correspondent.LastName = data.Form.LastName
provided.Correspondent.Email = data.Form.Email
Expand All @@ -56,6 +61,7 @@ func EnterCorrespondentDetails(tmpl template.Template, donorStore DonorStore, ev

if err := eventClient.SendCorrespondentUpdated(r.Context(), event.CorrespondentUpdated{
UID: provided.LpaUID,
ActorUID: &provided.Correspondent.UID,
FirstNames: provided.Correspondent.FirstNames,
LastName: provided.Correspondent.LastName,
Email: provided.Correspondent.Email,
Expand Down
Loading

0 comments on commit 7e754c2

Please sign in to comment.