Skip to content

Commit

Permalink
Added an event msg dispatch for created school contact.
Browse files Browse the repository at this point in the history
  • Loading branch information
chris.ditcher authored and chris.ditcher committed May 10, 2024
1 parent b3dd789 commit 219b7af
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum EventOutcome {

SCHOOL_NOT_FOUND,

SCHOOL_MOVED
SCHOOL_MOVED,
CONTACT_CREATED

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public enum EventType {

GET_PAGINATED_AUTHORITIES,

MOVE_SCHOOL
MOVE_SCHOOL,
CREATE_CONTACT
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ public SchoolContact getSchoolContact(UUID schoolId, UUID contactId) {
}

@Override
public SchoolContact createSchoolContact(UUID schoolId, SchoolContact contact) {
public SchoolContact createSchoolContact(UUID schoolId, SchoolContact contact) throws JsonProcessingException {
validatePayload(() -> this.contactPayloadValidator.validateCreatePayload(contact));
RequestUtil.setAuditColumnsForCreate(contact);
return schoolContactMapper.toStructure(schoolService.createSchoolContact(contact, schoolId));
var pair = schoolService.createSchoolContact(contact, schoolId);
publisher.dispatchChoreographyEvent(pair.getRight());
return schoolContactMapper.toStructure(pair.getLeft());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public interface SchoolAPIEndpoint {
@Tag(name = "School Contact Entity", description = "Endpoints for school contact entity.")
@Schema(name = "SchoolContact", implementation = SchoolContact.class)
@ResponseStatus(CREATED)
SchoolContact createSchoolContact(@PathVariable UUID schoolId, @Validated @RequestBody SchoolContact contact);
SchoolContact createSchoolContact(@PathVariable UUID schoolId, @Validated @RequestBody SchoolContact contact) throws JsonProcessingException;

@PutMapping("/{schoolId}/contact/{contactId}")
@PreAuthorize("hasAuthority('SCOPE_WRITE_SCHOOL_CONTACT')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import ca.bc.gov.educ.api.institute.mapper.v1.SchoolMapper;
import ca.bc.gov.educ.api.institute.model.v1.*;
import ca.bc.gov.educ.api.institute.repository.v1.*;
import ca.bc.gov.educ.api.institute.struct.v1.MoveSchoolData;
import ca.bc.gov.educ.api.institute.struct.v1.Note;
import ca.bc.gov.educ.api.institute.struct.v1.School;
import ca.bc.gov.educ.api.institute.struct.v1.SchoolContact;
import ca.bc.gov.educ.api.institute.struct.v1.*;
import ca.bc.gov.educ.api.institute.util.EventUtil;
import ca.bc.gov.educ.api.institute.util.JsonUtil;
import ca.bc.gov.educ.api.institute.util.RequestUtil;
Expand Down Expand Up @@ -258,15 +255,21 @@ public Optional<SchoolContactEntity> getSchoolContact(UUID schoolId, UUID contac
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public SchoolContactEntity createSchoolContact(SchoolContact contact, UUID schoolId) {
public Pair<SchoolContactEntity, InstituteEvent> createSchoolContact(SchoolContact contact, UUID schoolId) throws JsonProcessingException {
var contactEntity = SchoolContactMapper.mapper.toModel(contact);
Optional<SchoolEntity> curSchoolEntityOptional = schoolRepository.findById(schoolId);

if (curSchoolEntityOptional.isPresent()) {
contactEntity.setSchoolEntity(curSchoolEntityOptional.get());
TransformUtil.uppercaseFields(contactEntity);
schoolContactRepository.save(contactEntity);
return contactEntity;
final InstituteEvent instituteEvent = EventUtil.createInstituteEvent(
contact.getCreateUser(), contact.getUpdateUser(),
JsonUtil.getJsonStringFromObject(SchoolContactMapper.mapper.toStructure(contactEntity)),
CREATE_CONTACT, CONTACT_CREATED
);
instituteEventRepository.save(instituteEvent);
return Pair.of(contactEntity, instituteEvent);
} else {
throw new EntityNotFoundException(SchoolEntity.class, SCHOOL_ID_ATTR,
String.valueOf(schoolId));
Expand Down

0 comments on commit 219b7af

Please sign in to comment.