diff --git a/Tithe-Spring/src/main/java/com/tithe/controller/mutation/FamilyMutations.java b/Tithe-Spring/src/main/java/com/tithe/controller/mutation/FamilyMutations.java index d0c5dcf..5bcb6cf 100755 --- a/Tithe-Spring/src/main/java/com/tithe/controller/mutation/FamilyMutations.java +++ b/Tithe-Spring/src/main/java/com/tithe/controller/mutation/FamilyMutations.java @@ -3,6 +3,9 @@ */ package com.tithe.controller.mutation; + +import java.util.List; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.graphql.data.method.annotation.Argument; import org.springframework.graphql.data.method.annotation.MutationMapping; @@ -11,8 +14,10 @@ import com.tithe.entity.FamilyEntity; import com.tithe.entity.KoottaymaEntity; import com.tithe.model.FamilyMutationInput; +import com.tithe.model.PersonRelationInputModel; import com.tithe.service.mutation.FamilyMutationService; + /** * @author Ashish Sam T George * @@ -24,15 +29,23 @@ public class FamilyMutations { private FamilyMutationService familyMutationService; @MutationMapping(name = "createOneFamily") - public FamilyEntity createOneFamily(@Argument(name = "family") FamilyMutationInput familyMutationInput) { + public FamilyEntity createOneFamily( + @Argument(name = "family") FamilyMutationInput familyMutationInput) { return familyMutationService.createOneFamily(familyMutationInput); } - + + @MutationMapping(name = "changeHeadOfFamily") + public FamilyEntity changeHeadOfFamily(@Argument(name = "familyId") Long familyId, + @Argument(name = "newHeadOfFamily") PersonRelationInputModel headOfFamily, + @Argument(name = "persons") List persons) { + return familyMutationService.changeHeadOfFamily(familyId, headOfFamily, persons); + } + @MutationMapping(name = "activateOneFamily") public FamilyEntity activateOneFamily(@Argument(name = "familyId") Long familyId) { return familyMutationService.activateOneFamily(familyId); } - + @MutationMapping(name = "deactivateOneFamily") public FamilyEntity deactivateOneFamily(@Argument(name = "familyId") Long familyId) { return familyMutationService.deactivateOneFamily(familyId); diff --git a/Tithe-Spring/src/main/java/com/tithe/controller/mutation/PersonMutations.java b/Tithe-Spring/src/main/java/com/tithe/controller/mutation/PersonMutations.java index e3ce08d..21f85c0 100755 --- a/Tithe-Spring/src/main/java/com/tithe/controller/mutation/PersonMutations.java +++ b/Tithe-Spring/src/main/java/com/tithe/controller/mutation/PersonMutations.java @@ -3,6 +3,7 @@ */ package com.tithe.controller.mutation; + import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -23,6 +24,7 @@ import com.tithe.service.mutation.RelationMutationService; import com.tithe.service.mutation.TitheMutationService; + /** * @author Ashish Sam T George * @@ -46,10 +48,23 @@ public class PersonMutations { private TitheMutationService titheMutationService; @MutationMapping(name = "createOnePerson") - public PersonEntity createOnePerson(@Argument(name = "person") PersonMutationInput personMutationInput) { + public PersonEntity createOnePerson( + @Argument(name = "person") PersonMutationInput personMutationInput) { return personMutationService.createOnePerson(personMutationInput); } + @MutationMapping(name = "createManyPersonsInOneFamily") + public List createManyPersons(@Argument(name = "familyId") Long familyId, + @Argument(name = "persons") List personMutationInputs) { + return personMutationService.createManyPersonsInOneFamily(familyId, personMutationInputs); + } + + @MutationMapping(name = "changeRelation") + public PersonEntity changeRelation(@Argument(name = "personId") Long personId, + @Argument(name = "relationId") Long relationId) { + return personMutationService.changeRelation(personId, relationId); + } + @MutationMapping(name = "activateOnePerson") public PersonEntity activateOnePerson(@Argument Long id) { return personMutationService.activateOnePerson(id); @@ -73,24 +88,29 @@ public List deactivateManyPersons(@Argument List ids) { @MutationMapping(name = "createOneEducation") public EducationEntity createOneEducation(@Argument(name = "education") String educationName) { List educationNames = List.of(educationName); - List educations = educationMutationService.createManyEducations(educationNames); + List educations = educationMutationService + .createManyEducations(educationNames); return educations.get(0); } @MutationMapping(name = "createManyEducations") - public List createManyEducations(@Argument(name = "educations") List educationNames) { + public List createManyEducations( + @Argument(name = "educations") List educationNames) { return educationMutationService.createManyEducations(educationNames); } @MutationMapping(name = "createOneOccupation") - public OccupationEntity createOneOccupation(@Argument(name = "occupation") String occupationName) { + public OccupationEntity createOneOccupation( + @Argument(name = "occupation") String occupationName) { List occupationNames = List.of(occupationName); - List occupations = occupationMutationService.createManyOccupations(occupationNames); + List occupations = occupationMutationService + .createManyOccupations(occupationNames); return occupations.get(0); } @MutationMapping(name = "createManyOccupations") - public List createManyOccupations(@Argument(name = "occupations") List occupationNames) { + public List createManyOccupations( + @Argument(name = "occupations") List occupationNames) { return occupationMutationService.createManyOccupations(occupationNames); } @@ -102,7 +122,8 @@ public RelationEntity createOneRelation(@Argument(name = "relation") String rela } @MutationMapping(name = "createManyRelations") - public List createManyRelations(@Argument(name = "relations") List relationNames) { + public List createManyRelations( + @Argument(name = "relations") List relationNames) { return relationMutationService.createManyRelations(relationNames); } @@ -110,7 +131,8 @@ public List createManyRelations(@Argument(name = "relations") Li public TitheEntity createOneTithe(@Argument(name = "personId") Long personId, @Argument(name = "tithe") TitheMutationInput titheMutationInput) { List titheMutationInputs = List.of(titheMutationInput); - List tithes = titheMutationService.createManyTithes(personId, titheMutationInputs); + List tithes = titheMutationService.createManyTithes(personId, + titheMutationInputs); return tithes.get(0); } diff --git a/Tithe-Spring/src/main/java/com/tithe/entity/FamilyEntity.java b/Tithe-Spring/src/main/java/com/tithe/entity/FamilyEntity.java index 207fec2..36de3a9 100755 --- a/Tithe-Spring/src/main/java/com/tithe/entity/FamilyEntity.java +++ b/Tithe-Spring/src/main/java/com/tithe/entity/FamilyEntity.java @@ -3,6 +3,7 @@ */ package com.tithe.entity; + import java.util.List; import jakarta.persistence.CascadeType; @@ -13,6 +14,7 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; +import jakarta.persistence.OneToOne; import jakarta.persistence.Table; import jakarta.persistence.UniqueConstraint; import jakarta.validation.constraints.NotBlank; @@ -21,6 +23,7 @@ import lombok.Data; import lombok.NoArgsConstructor; + /** * @author Ashish Sam T George * @@ -29,7 +32,8 @@ @AllArgsConstructor @NoArgsConstructor @Entity -@Table(name = "family_table", uniqueConstraints = @UniqueConstraint(columnNames = {"familyName", "phone"})) +@Table(name = "family_table", uniqueConstraints = @UniqueConstraint(columnNames = { "familyName", + "head_of_family_id", "phone" })) public class FamilyEntity { @Id @@ -51,7 +55,12 @@ public class FamilyEntity { @JoinColumn(name = "koottayma_id") private KoottaymaEntity koottayma; - @OneToMany(mappedBy = "family") + @NotNull(message = "Head of Family does not exist") + @OneToOne(cascade = CascadeType.PERSIST) + @JoinColumn(name = "head_of_family_id") + private PersonEntity headOfFamily; + + @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "family") private List persons; @OneToMany(mappedBy = "family") diff --git a/Tithe-Spring/src/main/java/com/tithe/entity/OccupationEntity.java b/Tithe-Spring/src/main/java/com/tithe/entity/OccupationEntity.java index abbe959..87babe4 100644 --- a/Tithe-Spring/src/main/java/com/tithe/entity/OccupationEntity.java +++ b/Tithe-Spring/src/main/java/com/tithe/entity/OccupationEntity.java @@ -5,6 +5,8 @@ import java.util.List; +import com.tithe.model.OccupationSectorEnum; + import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -13,6 +15,7 @@ import jakarta.persistence.ManyToMany; import jakarta.persistence.Table; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; diff --git a/Tithe-Spring/src/main/java/com/tithe/entity/PersonBuilder.java b/Tithe-Spring/src/main/java/com/tithe/entity/PersonBuilder.java new file mode 100755 index 0000000..169d456 --- /dev/null +++ b/Tithe-Spring/src/main/java/com/tithe/entity/PersonBuilder.java @@ -0,0 +1,114 @@ +/** + * + */ +package com.tithe.entity; + + +import java.time.LocalDate; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; + +import com.tithe.model.GenderEnum; +import com.tithe.model.OccupationSectorEnum; +import com.tithe.utils.ObjectValidation; + + +/** + * @author Ashish Sam T George + * + */ +public class PersonBuilder { + + private Long personId; + private String baptismName; + private String personName; + private FamilyEntity family; + private RelationEntity relation; + private GenderEnum gender; + private LocalDate dob; + private String phone; + private List tithes; + private Boolean moved = false; + private List educations; + private OccupationSectorEnum occupationSector; + private List occupations; + private Boolean active = true; + + public PersonBuilder personId(Long personId) { + this.personId = personId; + return this; + } + + public PersonBuilder baptismName(String baptismName) { + this.baptismName = baptismName; + return this; + } + + public PersonBuilder personName(String personName) { + this.personName = personName; + return this; + } + + public PersonBuilder family(FamilyEntity family) { + this.family = family; + return this; + } + + public PersonBuilder relation(RelationEntity relation) { + this.relation = relation; + return this; + } + + public PersonBuilder gender(GenderEnum gender) { + this.gender = gender; + return this; + } + + public PersonBuilder dob(LocalDate dob) { + this.dob = dob; + return this; + } + + public PersonBuilder phone(String phone) { + this.phone = phone; + return this; + } + +// public PersonBuilder tithes(List tithes) { +// this.tithes = tithes; +// return this; +// } + + public PersonBuilder moved(Boolean moved) { + this.moved = moved; + return this; + } + + public PersonBuilder educations(List educations) { + this.educations = educations; + return this; + } + + public PersonBuilder occupationSector(OccupationSectorEnum occupationSector) { + this.occupationSector = occupationSector; + return this; + } + + public PersonBuilder occupations(List occupations) { + this.occupations = occupations; + return this; + } + + public PersonBuilder active(Boolean active) { + this.active = active; + return this; + } + + public PersonEntity build() { + PersonEntity person = new PersonEntity(personId, baptismName, personName, family, relation, gender, dob, phone, + tithes, moved, educations, occupationSector, occupations, active); + return person; + } + +} diff --git a/Tithe-Spring/src/main/java/com/tithe/entity/PersonEntity.java b/Tithe-Spring/src/main/java/com/tithe/entity/PersonEntity.java index fbad0e5..7290c97 100755 --- a/Tithe-Spring/src/main/java/com/tithe/entity/PersonEntity.java +++ b/Tithe-Spring/src/main/java/com/tithe/entity/PersonEntity.java @@ -7,6 +7,7 @@ import java.util.List; import com.tithe.model.GenderEnum; +import com.tithe.model.OccupationSectorEnum; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -53,8 +54,9 @@ public class PersonEntity { @NotBlank(message = "Name of Person is empty or null") private String personName; - @NotNull(message = "Family does not exist") - @ManyToOne(cascade = CascadeType.PERSIST) +// @NotNull(message = "Family does not exist") +// @ManyToOne(cascade = CascadeType.PERSIST) + @ManyToOne @JoinColumn(name = "family_id") private FamilyEntity family; @@ -84,6 +86,8 @@ public class PersonEntity { @JoinTable(name = "person_educations_table", uniqueConstraints = @UniqueConstraint(columnNames = {"persons_person_id", "educations_education_id"})) private List educations; + + private OccupationSectorEnum occupationSector; @ManyToMany(cascade = CascadeType.PERSIST) @JoinTable(name = "person_occupations_table", joinColumns = @JoinColumn(name = "person_id"), diff --git a/Tithe-Spring/src/main/java/com/tithe/entity/TitheBuilder.java b/Tithe-Spring/src/main/java/com/tithe/entity/TitheBuilder.java new file mode 100755 index 0000000..0c54423 --- /dev/null +++ b/Tithe-Spring/src/main/java/com/tithe/entity/TitheBuilder.java @@ -0,0 +1,98 @@ +/** + * + */ +package com.tithe.entity; + + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +import com.tithe.model.TitheMutationInput; + + +/** + * @author Ashish Sam T George + * + */ +public class TitheBuilder { + + private Long titheId; + private Double titheAmount; + private PersonEntity person; + private FamilyEntity family; + private KoottaymaEntity koottayma; + private ParishEntity parish; + private ForaneEntity forane; + private LocalDate timeStamp; + + public TitheBuilder titheId(Long titheId) { + this.titheId = titheId; + return this; + } + + public TitheBuilder titheAmount(Double titheAmount) { + this.titheAmount = titheAmount; + return this; + } + + public TitheBuilder person(PersonEntity person) { + this.person = person; + return this; + } + + public TitheBuilder family(FamilyEntity family) { + this.family = family; + return this; + } + + public TitheBuilder koottayma(KoottaymaEntity koottayma) { + this.koottayma = koottayma; + return this; + } + + public TitheBuilder parish(ParishEntity parish) { + this.parish = parish; + return this; + } + + public TitheBuilder forane(ForaneEntity forane) { + this.forane = forane; + return this; + } + + public TitheBuilder timeStamp(LocalDate timeStamp) { + this.timeStamp = timeStamp; + return this; + } + + public TitheEntity build() { + TitheEntity tithe = new TitheEntity(titheId, titheAmount, person, family, koottayma, parish, + forane, timeStamp); + return tithe; + } + + public List buildTithe(PersonEntity person, + List titheMutationInputs) { + List tithes = new ArrayList<>(); + if (titheMutationInputs != null && titheMutationInputs.size() != 0) { +// objectValidation.validateObjects(titheMutationInputs); + + for (TitheMutationInput titheMutationInput : titheMutationInputs) { +// New Builder object for each tithe + TitheBuilder titheBuilder = new TitheBuilder(); + TitheEntity tithe = titheBuilder.titheAmount(titheMutationInput.getTitheAmount()) + .person(person) + .family(person.getFamily()) + .koottayma(person.getFamily().getKoottayma()) + .parish(person.getFamily().getKoottayma().getParish()) + .forane(person.getFamily().getKoottayma().getParish().getForane()) + .timeStamp(titheMutationInput.getTimeStamp()) + .build(); + tithes.add(tithe); + } + } + return tithes; + } + +} diff --git a/Tithe-Spring/src/main/java/com/tithe/model/FamilyMutationInput.java b/Tithe-Spring/src/main/java/com/tithe/model/FamilyMutationInput.java index 9f43f55..5bd7243 100755 --- a/Tithe-Spring/src/main/java/com/tithe/model/FamilyMutationInput.java +++ b/Tithe-Spring/src/main/java/com/tithe/model/FamilyMutationInput.java @@ -3,6 +3,8 @@ */ package com.tithe.model; +import java.util.List; + import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Positive; @@ -26,6 +28,11 @@ public class FamilyMutationInput { @Positive(message = "Id of Koottayma should be valid") @NotNull(message = "Id of Koottayma should not be empty or null") Long koottaymaId; + + @NotNull(message = "Head of Family should not be empty or null") + PersonMutationInput headOfFamily; + + List persons; Boolean active = true; diff --git a/Tithe-Spring/src/main/java/com/tithe/model/OccupationSectorEnum.java b/Tithe-Spring/src/main/java/com/tithe/model/OccupationSectorEnum.java new file mode 100755 index 0000000..e9a69a8 --- /dev/null +++ b/Tithe-Spring/src/main/java/com/tithe/model/OccupationSectorEnum.java @@ -0,0 +1,21 @@ +/** + * + */ +package com.tithe.model; + +/** + * @author Ashish Sam T George + * + */ +public enum OccupationSectorEnum { + + GOVERNMENT("Government"), + PRIVATE("Private"); + + private String sector; + + OccupationSectorEnum(String sector) { + this.sector = sector; + } + +} diff --git a/Tithe-Spring/src/main/java/com/tithe/model/PersonMutationInput.java b/Tithe-Spring/src/main/java/com/tithe/model/PersonMutationInput.java index fa600de..0dcbc94 100755 --- a/Tithe-Spring/src/main/java/com/tithe/model/PersonMutationInput.java +++ b/Tithe-Spring/src/main/java/com/tithe/model/PersonMutationInput.java @@ -27,8 +27,8 @@ public class PersonMutationInput { @NotBlank(message = "Name of Person should not be empty or null") String personName; - @Positive(message = "Id of Family should be valid") - @NotNull(message = "Id of Family should not be empty or null") +// @Positive(message = "Id of Family should be valid") +// @NotNull(message = "Id of Family should not be empty or null") Long familyId; @Positive(message = "Id of Relation should be valid") @@ -46,6 +46,9 @@ public class PersonMutationInput { List tithes; Boolean moved = false; List educationIds; + + OccupationSectorEnum occupationSector; + List occupationIds; Boolean active = true; diff --git a/Tithe-Spring/src/main/java/com/tithe/model/PersonRelationInputModel.java b/Tithe-Spring/src/main/java/com/tithe/model/PersonRelationInputModel.java new file mode 100755 index 0000000..40539aa --- /dev/null +++ b/Tithe-Spring/src/main/java/com/tithe/model/PersonRelationInputModel.java @@ -0,0 +1,27 @@ +/** + * + */ +package com.tithe.model; + +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Positive; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author Ashish Sam T George + * + */ +@NoArgsConstructor +@Data +public class PersonRelationInputModel { + + @Positive(message = "Id of Person should be valid") + @NotNull(message = "Id of Person should not be empty or null") + private Long personId; + + @Positive(message = "Id of Relation should be valid") + @NotNull(message = "Id of Relation should not be empty or null") + private Long relationId; + +} diff --git a/Tithe-Spring/src/main/java/com/tithe/service/mutation/FamilyMutationService.java b/Tithe-Spring/src/main/java/com/tithe/service/mutation/FamilyMutationService.java index 61304e7..a493a9c 100755 --- a/Tithe-Spring/src/main/java/com/tithe/service/mutation/FamilyMutationService.java +++ b/Tithe-Spring/src/main/java/com/tithe/service/mutation/FamilyMutationService.java @@ -3,6 +3,9 @@ */ package com.tithe.service.mutation; + +import java.util.Iterator; +import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; @@ -10,9 +13,11 @@ import com.tithe.entity.AddressEntity; import com.tithe.entity.FamilyEntity; -import com.tithe.entity.KoottaymaEntity; +import com.tithe.entity.PersonEntity; import com.tithe.model.AddressMutationInput; import com.tithe.model.FamilyMutationInput; +import com.tithe.model.PersonMutationInput; +import com.tithe.model.PersonRelationInputModel; import com.tithe.repository.FamilyRepository; import com.tithe.service.query.AddressQueryService; import com.tithe.service.query.KoottaymaQueryService; @@ -20,6 +25,7 @@ import graphql.GraphQLException; + /** * @author Ashish Sam T George * @@ -36,6 +42,9 @@ public class FamilyMutationService { @Autowired private KoottaymaQueryService koottaymaQueryService; + @Autowired + private PersonMutationService personMutationService; + @Autowired private FamilyRepository familyRepository; @@ -46,44 +55,91 @@ public FamilyEntity createOneFamily(FamilyMutationInput familyMutationInput) { FamilyEntity family = new FamilyEntity(); family.setFamilyName(familyMutationInput.getFamilyName()); +// If the same address already exists, then set that address to family entity or else build a new address +// entity and set it to family entity AddressMutationInput addressInput = familyMutationInput.getAddress(); - if (addressInput!=null) { + if (addressInput != null) { AddressEntity address = addressQueryService.getOneAddress(addressInput); - if (address!=null) { + if (address != null) { family.setAddress(address); - } - else { + } else { family.setAddress(addressQueryService.buildAddressEntity(addressInput)); } } family.setPhone(familyMutationInput.getPhone()); - family.setKoottayma(koottaymaQueryService.getOneKoottayma(familyMutationInput.getKoottaymaId())); + family.setKoottayma( + koottaymaQueryService.getOneKoottayma(familyMutationInput.getKoottaymaId())); + +// Build Head of Family person entity + PersonMutationInput headOfFamilyInput = familyMutationInput.getHeadOfFamily(); + PersonEntity headOfFamily = personMutationService.buildPersonWithTithe(family, + headOfFamilyInput); + family.setHeadOfFamily(headOfFamily); + +// Build Family members + List familyMemberInputs = familyMutationInput.getPersons(); + if (familyMemberInputs != null) { + + objectValidation.validateObjects(familyMemberInputs); + List familyMembers = personMutationService.buildPersonsWithTithe(family, + familyMemberInputs); + family.setPersons(familyMembers); + } family.setActive(familyMutationInput.getActive()); return familyRepository.save(family); } + public FamilyEntity changeHeadOfFamily(Long familyId, + PersonRelationInputModel headOfFamilyModel, + List personModels) { + + objectValidation.validateObject(headOfFamilyModel); + objectValidation.validateObject(personModels); + + if (familyId == null) { + throw new GraphQLException("Family Id is invalid"); + } + + Optional obtainedFamily = familyRepository.findById(familyId); + FamilyEntity family = obtainedFamily.orElseThrow(); + + PersonEntity headOfFamily = personMutationService + .changeRelation(headOfFamilyModel.getPersonId(), headOfFamilyModel.getRelationId()); + family.setHeadOfFamily(headOfFamily); + + if (personModels != null && personModels.size() != 0) { + for (PersonRelationInputModel personModel : personModels) { + personMutationService.changeRelation(personModel.getPersonId(), + personModel.getRelationId()); + } + } + + return family; + + } + public FamilyEntity activateOneFamily(Long familyId) { - if (familyId!=null) { + if (familyId != null) { Optional obtainedFamily = familyRepository.findById(familyId); FamilyEntity family = obtainedFamily.orElseThrow(); family.setActive(true); return familyRepository.save(family); } - GraphQLException exception = new GraphQLException("Some Error Occured") ; + GraphQLException exception = new GraphQLException("Some Error Occured"); throw exception; } public FamilyEntity deactivateOneFamily(Long familyId) { - if (familyId!=null) { + if (familyId != null) { Optional obtainedFamily = familyRepository.findById(familyId); FamilyEntity family = obtainedFamily.orElseThrow(); family.setActive(false); return familyRepository.save(family); } - GraphQLException exception = new GraphQLException("Some Error Occured") ; + GraphQLException exception = new GraphQLException("Some Error Occured"); throw exception; } diff --git a/Tithe-Spring/src/main/java/com/tithe/service/mutation/PersonMutationService.java b/Tithe-Spring/src/main/java/com/tithe/service/mutation/PersonMutationService.java index 1bdc6dd..ff2dd33 100755 --- a/Tithe-Spring/src/main/java/com/tithe/service/mutation/PersonMutationService.java +++ b/Tithe-Spring/src/main/java/com/tithe/service/mutation/PersonMutationService.java @@ -3,14 +3,22 @@ */ package com.tithe.service.mutation; + import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.tithe.entity.EducationEntity; +import com.tithe.entity.FamilyEntity; +import com.tithe.entity.OccupationEntity; +import com.tithe.entity.PersonBuilder; import com.tithe.entity.PersonEntity; +import com.tithe.entity.RelationEntity; +import com.tithe.entity.TitheBuilder; import com.tithe.entity.TitheEntity; import com.tithe.model.PersonMutationInput; import com.tithe.model.TitheMutationInput; @@ -18,10 +26,14 @@ import com.tithe.service.query.EducationQueryService; import com.tithe.service.query.FamilyQueryService; import com.tithe.service.query.OccupationQueryService; +import com.tithe.service.query.PersonQueryService; import com.tithe.service.query.RelationQueryService; import com.tithe.service.query.TitheQueryService; import com.tithe.utils.ObjectValidation; +import graphql.GraphQLException; + + /** * @author Ashish Sam T George * @@ -35,6 +47,9 @@ public class PersonMutationService { @Autowired private PersonRepository personRepository; + @Autowired + private PersonQueryService personQueryService; + @Autowired private FamilyQueryService familyQueryService; @@ -52,35 +67,44 @@ public class PersonMutationService { public PersonEntity createOnePerson(PersonMutationInput personMutationInput) { +// Validate the obtained input objectValidation.validateObject(personMutationInput); - PersonEntity person = new PersonEntity(); - person.setBaptismName(personMutationInput.getBaptismName()); - person.setPersonName(personMutationInput.getPersonName()); - person.setFamily(familyQueryService.getOneFamily(personMutationInput.getFamilyId())); - person.setRelation(relationQueryService.getOneRelation(personMutationInput.getRelationId())); - person.setGender(personMutationInput.getGender()); - person.setDob(personMutationInput.getDob()); - person.setPhone(personMutationInput.getPhone()); - - List titheInputs = personMutationInput.getTithes(); - List tithes = new ArrayList<>(); - if (titheInputs!=null && titheInputs.size()!=0) { - tithes = titheQueryService.buildTitheEntities(person, titheInputs); +// Get family using the id, if exists, or else return error + FamilyEntity family = familyQueryService.getOneFamily(personMutationInput.getFamilyId()); + if (family == null) { + throw new GraphQLException("Family does not exist"); } - person.setTithes(tithes); - person.setMoved(personMutationInput.getMoved()); + PersonEntity person = buildPersonWithTithe(family, personMutationInput); - if (personMutationInput.getEducationIds().size()!=0) { - person.setEducations(educationQueryService.getManyEducations(personMutationInput.getEducationIds())); + return personRepository.save(person); + } + + public List createManyPersonsInOneFamily(Long familyId, + List personMutationInputs) { + FamilyEntity family = familyQueryService.getOneFamily(familyId); + + if (family != null) { + List persons = buildPersonsWithTithe(family, personMutationInputs); + return personRepository.saveAll(persons); } - if (personMutationInput.getOccupationIds().size()!=0) { - person.setOccupations(occupationQueryService.getManyOccupations(personMutationInput.getOccupationIds())); + throw new GraphQLException("Family does not exist"); + } + + public PersonEntity changeRelation(Long personId, Long relationId) { + if (personId == null || relationId == null) { + throw new GraphQLException("Person Id and Relation Id should be valid"); } - person.setActive(personMutationInput.getActive()); + PersonEntity person = personQueryService.getOnePerson(personId); + RelationEntity relation = relationQueryService.getOneRelation(relationId); + + if (person == null || relation == null) { + throw new GraphQLException("Person and Relation should exist"); + } + person.setRelation(relation); return personRepository.save(person); } @@ -95,7 +119,7 @@ public PersonEntity activateOnePerson(Long id) { public List activateManyPersons(List ids) { List persons = personRepository.findAllById(ids); - if (persons.size()!=0) { + if (persons.size() != 0) { for (PersonEntity person : persons) { person.setActive(true); } @@ -116,7 +140,7 @@ public PersonEntity deactivateOnePerson(Long id) { public List deactivateManyPersons(List ids) { List persons = personRepository.findAllById(ids); - if (persons.size()!=0) { + if (persons.size() != 0) { for (PersonEntity person : persons) { person.setActive(false); } @@ -125,4 +149,64 @@ public List deactivateManyPersons(List ids) { return null; } + public List getEducations(List educationIds) { + List educations = new ArrayList<>(); + if (educationIds != null && educationIds.size() != 0) { + educations = educationQueryService.getManyEducations(educationIds); + } + return educations; + } + + public List getOccupations(List occupationIds) { + List occupations = new ArrayList<>(); + if (occupationIds != null && occupationIds.size() != 0) { + occupations = occupationQueryService.getManyOccupations(occupationIds); + } + return occupations; + } + + public PersonEntity buildPersonWithTithe(FamilyEntity family, + PersonMutationInput personMutationInput) { + objectValidation.validateObject(personMutationInput); + + RelationEntity relation = relationQueryService + .getOneRelation(personMutationInput.getRelationId()); + + List educations = getEducations(personMutationInput.getEducationIds()); + + List occupations = getOccupations(personMutationInput.getOccupationIds()); + + PersonBuilder personBuilder = new PersonBuilder(); + PersonEntity person = personBuilder.baptismName(personMutationInput.getBaptismName()) + .personName(personMutationInput.getPersonName()) + .family(family) + .relation(relation) + .gender(personMutationInput.getGender()) + .dob(personMutationInput.getDob()) + .phone(personMutationInput.getPhone()) + .moved(personMutationInput.getMoved()) + .educations(educations) + .occupationSector(personMutationInput.getOccupationSector()) + .occupations(occupations) + .active(personMutationInput.getActive()) + .build(); + +// Build Tithe entity + TitheBuilder titheBuilder = new TitheBuilder(); + List tithes = titheBuilder.buildTithe(person, personMutationInput.getTithes()); + person.setTithes(tithes); + + return person; + } + + public List buildPersonsWithTithe(FamilyEntity family, + List personMutationInputs) { + List persons = new ArrayList<>(); + for (PersonMutationInput personMutationInput : personMutationInputs) { + PersonEntity person = buildPersonWithTithe(family, personMutationInput); + persons.add(person); + } + return persons; + } + } diff --git a/Tithe-Spring/src/main/java/com/tithe/service/mutation/TitheMutationService.java b/Tithe-Spring/src/main/java/com/tithe/service/mutation/TitheMutationService.java index edb15bf..f8fce65 100755 --- a/Tithe-Spring/src/main/java/com/tithe/service/mutation/TitheMutationService.java +++ b/Tithe-Spring/src/main/java/com/tithe/service/mutation/TitheMutationService.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Service; import com.tithe.entity.PersonEntity; +import com.tithe.entity.TitheBuilder; import com.tithe.entity.TitheEntity; import com.tithe.model.TitheMutationInput; import com.tithe.repository.TitheRepository; @@ -36,7 +37,8 @@ public List createManyTithes(Long personId, // TODO Try adding @NotNull in the method parameter above - No Use PersonEntity person = personQueryService.getOnePerson(personId); - List tithes = titheQueryService.buildTitheEntities(person, titheMutationInputs); + TitheBuilder titheBuilder = new TitheBuilder(); + List tithes = titheBuilder.buildTithe(person, titheMutationInputs); return titheRepository.saveAll(tithes); } diff --git a/Tithe-Spring/src/main/java/com/tithe/service/query/PersonQueryService.java b/Tithe-Spring/src/main/java/com/tithe/service/query/PersonQueryService.java index 148d9ec..0bc00de 100755 --- a/Tithe-Spring/src/main/java/com/tithe/service/query/PersonQueryService.java +++ b/Tithe-Spring/src/main/java/com/tithe/service/query/PersonQueryService.java @@ -39,6 +39,7 @@ public class PersonQueryService { @Autowired private FamilyQueryService familyQueryService; + public PersonEntity getOnePerson(Long id) { Optional person = personRepository.findById(id); diff --git a/Tithe-Spring/src/main/java/com/tithe/service/query/TitheQueryService.java b/Tithe-Spring/src/main/java/com/tithe/service/query/TitheQueryService.java index 9e94995..5fd23c2 100755 --- a/Tithe-Spring/src/main/java/com/tithe/service/query/TitheQueryService.java +++ b/Tithe-Spring/src/main/java/com/tithe/service/query/TitheQueryService.java @@ -3,17 +3,8 @@ */ package com.tithe.service.query; -import java.util.ArrayList; -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.tithe.entity.PersonEntity; -import com.tithe.entity.TitheEntity; -import com.tithe.model.TitheMutationInput; -import com.tithe.utils.ObjectValidation; - /** * @author Ashish Sam T George * @@ -21,30 +12,4 @@ @Service public class TitheQueryService { - @Autowired - private ObjectValidation objectValidation; - - public List buildTitheEntities(PersonEntity person, - List titheMutationInputs) { - - objectValidation.validateObjects(titheMutationInputs); - - List tithes = new ArrayList<>(); - for (TitheMutationInput titheInput : titheMutationInputs) { - TitheEntity tithe = new TitheEntity(); - tithe.setTitheAmount(titheInput.getTitheAmount()); - tithe.setTimeStamp(titheInput.getTimeStamp()); - tithe.setPerson(person); - tithe.setFamily(person.getFamily()); - tithe.setKoottayma(tithe.getFamily().getKoottayma()); - tithe.setParish(tithe.getKoottayma().getParish()); - tithe.setForane(tithe.getParish().getForane()); - - tithes.add(tithe); - } - - return tithes; - - } - } diff --git a/Tithe-Spring/src/main/resources/graphql/mutations/FamilyMutations.graphqls b/Tithe-Spring/src/main/resources/graphql/mutations/FamilyMutations.graphqls index 24ac54a..dcafb24 100755 --- a/Tithe-Spring/src/main/resources/graphql/mutations/FamilyMutations.graphqls +++ b/Tithe-Spring/src/main/resources/graphql/mutations/FamilyMutations.graphqls @@ -1,5 +1,8 @@ extend type Mutation{ - createOneFamily(family: FamilyMutationInput): Family + createOneFamily(family: FamilyMutationInput!): Family + + changeHeadOfFamily(familyId: ID!, newHeadOfFamily: PersonRelationSchema!, persons: [PersonRelationSchema]): Family + activateOneFamily(familyId: ID!): Family deactivateOneFamily(familyId: ID!): Family } diff --git a/Tithe-Spring/src/main/resources/graphql/mutations/PersonMutations.graphqls b/Tithe-Spring/src/main/resources/graphql/mutations/PersonMutations.graphqls index a039a57..c74696b 100755 --- a/Tithe-Spring/src/main/resources/graphql/mutations/PersonMutations.graphqls +++ b/Tithe-Spring/src/main/resources/graphql/mutations/PersonMutations.graphqls @@ -1,5 +1,8 @@ extend type Mutation{ - createOnePerson(person: PersonMutationInput): Person + createOnePerson(person: PersonMutationInput!): Person + createManyPersonsInOneFamily(familyId: ID!, persons: [PersonMutationThroughFamilyInput]!): [Person] + + changeRelation(personId: ID!, relationId: ID!): Person activateOnePerson(id: ID): Person activateManyPersons(ids: [ID]): [Person] diff --git a/Tithe-Spring/src/main/resources/graphql/types/Family.graphqls b/Tithe-Spring/src/main/resources/graphql/types/Family.graphqls index fc06189..57a32ec 100755 --- a/Tithe-Spring/src/main/resources/graphql/types/Family.graphqls +++ b/Tithe-Spring/src/main/resources/graphql/types/Family.graphqls @@ -4,6 +4,7 @@ type Family{ address: Address phone: String koottayma: Koottayma! + headOfFamily: Person persons: [Person] tithes: [Tithe] active: Boolean @@ -21,5 +22,7 @@ input FamilyMutationInput{ address: AddressMutationInput phone: String koottaymaId: ID! + headOfFamily: PersonMutationThroughFamilyInput! + persons: [PersonMutationThroughFamilyInput] active: Boolean = true } diff --git a/Tithe-Spring/src/main/resources/graphql/types/Person.graphqls b/Tithe-Spring/src/main/resources/graphql/types/Person.graphqls index 8e731ed..4ae800e 100755 --- a/Tithe-Spring/src/main/resources/graphql/types/Person.graphqls +++ b/Tithe-Spring/src/main/resources/graphql/types/Person.graphqls @@ -10,6 +10,7 @@ type Person{ tithes: [Tithe] moved: Boolean educations: [Education] + occupationSector: OccupationSectorEnum occupations: [Occupation] active: Boolean } @@ -34,10 +35,31 @@ input PersonMutationInput{ tithes: [TitheMutationInput] moved: Boolean = false educationIds: [ID] + occupationSector: OccupationSectorEnum occupationIds: [ID] active: Boolean = true } +input PersonMutationThroughFamilyInput{ + baptismName: String! + personName: String! + relationId: ID! + gender: GenderEnum! + dob: LocalDate! + phone: String + tithes: [TitheMutationInput] + moved: Boolean = false + educationIds: [ID] + occupationSector: OccupationSectorEnum + occupationIds: [ID] + active: Boolean = true +} + +input PersonRelationSchema{ + personId: ID! + relationId: ID! +} + type Relation{ relationId: ID! relationName: String! @@ -54,6 +76,11 @@ type Education{ persons: [Person] } +enum OccupationSectorEnum{ + GOVERNMENT + PRIVATE +} + type Occupation{ occupationId: ID! occupationName: String! diff --git a/Tithe-Vue/src/components/AddressForm.vue b/Tithe-Vue/src/components/AddressForm.vue index 17cf0bc..e63e156 100644 --- a/Tithe-Vue/src/components/AddressForm.vue +++ b/Tithe-Vue/src/components/AddressForm.vue @@ -19,7 +19,6 @@ import { } from "@/externalized-data/graphqlMutations"; import FormField from "@/components/FormField.vue"; -import SearchBox from "@/components/SearchBox.vue"; import SingleSelectBox from "@/components/SearchBoxes/SingleSelectBox.vue"; const emit = defineEmits(["addressFormChange"]); @@ -52,6 +51,7 @@ const { }, () => ({ enabled: streetListQueryEnabled, + fetchPolicy: "no-cache", }) ); @@ -102,7 +102,7 @@ const changeInStreet = (entity) => { }; watch(street, (value) => { - address.streetId = value.id; + address.streetId = value?.id ?? ""; emit("addressFormChange", address); }); @@ -118,17 +118,19 @@ const { onError: createStreetError, } = useMutation(CREATE_STREET_MUTATION); -const createStreetOption = (option, setSelected) => { +const createStreetOption = async (option, select$) => { createStreet({ streetName: option.label }); // Not using loading for now - createStreetDone((mutationResult) => { - setSelected({ - id: mutationResult.data?.createOneStreet?.streetId ?? "", - label: mutationResult.data?.createOneStreet?.streetName ?? "", + await new Promise((resolve, reject) => { + createStreetDone(() => { + select$.clear(); + resolve("Success"); }); }); + + return false; }; // City Search Box @@ -150,6 +152,7 @@ const { }, () => ({ enabled: cityListQueryEnabled, + fetchPolicy: "no-cache", }) ); @@ -200,7 +203,7 @@ const changeInCity = (entity) => { }; watch(city, (value) => { - address.cityId = value.id; + address.cityId = value?.id ?? ""; emit("addressFormChange", address); }); @@ -216,17 +219,17 @@ const { onError: createCityError, } = useMutation(CREATE_CITY_MUTATION); -const createCityOption = (option, setSelected) => { +const createCityOption = async (option, select$) => { createCity({ cityName: option.label }); - // Not using loading for now - - createCityDone((mutationResult) => { - setSelected({ - id: mutationResult.data?.createOneCity?.cityId ?? "", - label: mutationResult.data?.createOneCity?.cityName ?? "", + await new Promise((resolve, reject) => { + createCityDone(() => { + select$.clear(); + resolve("Success"); }); }); + + return false; }; // District Search Box @@ -248,6 +251,7 @@ const { }, () => ({ enabled: districtListQueryEnabled, + fetchPolicy: "no-cache", }) ); @@ -281,7 +285,7 @@ const changeInDistrict = (entity) => { }; watch(district, (value) => { - address.districtId = value.id; + address.districtId = value?.id ?? ""; emit("addressFormChange", address); }); @@ -297,17 +301,17 @@ const { onError: createDistrictError, } = useMutation(CREATE_DISTRICT_MUTATION); -const createDistrictOption = (option, setSelected) => { +const createDistrictOption = async (option, select$) => { createDistrict({ districtName: option.label }); - // Not using loading for now - - createDistrictDone((mutationResult) => { - setSelected({ - id: mutationResult.data?.createOneDistrict?.districtId ?? "", - label: mutationResult.data?.createOneDistrict?.districtName ?? "", + await new Promise((resolve, reject) => { + createDistrictDone(() => { + select$.clear(); + resolve("Success"); }); }); + + return false; }; // State Search Box @@ -329,6 +333,7 @@ const { }, () => ({ enabled: stateListQueryEnabled, + fetchPolicy: "no-cache", }) ); @@ -362,7 +367,7 @@ const changeInState = (entity) => { }; watch(state, (value) => { - address.stateId = value.id; + address.stateId = value?.id ?? ""; emit("addressFormChange", address); }); @@ -378,17 +383,17 @@ const { onError: createStateError, } = useMutation(CREATE_STATE_MUTATION); -const createStateOption = (option, setSelected) => { +const createStateOption = async (option, select$) => { createState({ stateName: option.label }); - // Not using loading for now - - createStateDone((mutationResult) => { - setSelected({ - id: mutationResult.data?.createOneState?.stateId ?? "", - label: mutationResult.data?.createOneState?.stateName ?? "", + await new Promise((resolve, reject) => { + createStateDone(() => { + select$.clear(); + resolve("Success"); }); }); + + return false; }; // Pincode Search Box @@ -410,6 +415,7 @@ const { }, () => ({ enabled: pincodeListQueryEnabled, + fetchPolicy: "no-cache", }) ); @@ -443,7 +449,7 @@ const changeInPincode = (entity) => { }; watch(pincode, (value) => { - address.pincodeId = value.id; + address.pincodeId = value?.id ?? ""; emit("addressFormChange", address); }); @@ -459,25 +465,37 @@ const { onError: createPincodeError, } = useMutation(CREATE_PINCODE_MUTATION); -const createPincodeOption = (option, setSelected) => { +const createPincodeOption = async (option, select$) => { createPincode({ pincode: option.label }); - // Not using loading for now - - createPincodeDone((mutationResult) => { - setSelected({ - id: mutationResult.data?.createOnePincode?.pincodeId ?? "", - label: mutationResult.data?.createOnePincode?.pincode ?? "", + await new Promise((resolve, reject) => { + createPincodeDone(() => { + select$.clear(); + resolve("Success"); }); }); + + return false; }; +const addressRef = ref(null); +const cityRef = ref(null); +const districtRef = ref(null); +const stateRef = ref(null); +const pincodeRef = ref(null); + const clearAddressFields = () => { street.value = ""; city.value = ""; district.value = ""; state.value = ""; pincode.value = ""; + + addressRef.value.clearField(); + cityRef.value.clearField(); + districtRef.value.clearField(); + stateRef.value.clearField(); + pincodeRef.value.clearField(); }; defineExpose({ @@ -500,12 +518,15 @@ defineExpose({ diff --git a/Tithe-Vue/src/components/Forms/AddPersonInFamilyForm.vue b/Tithe-Vue/src/components/Forms/AddPersonInFamilyForm.vue new file mode 100644 index 0000000..12f570a --- /dev/null +++ b/Tithe-Vue/src/components/Forms/AddPersonInFamilyForm.vue @@ -0,0 +1,63 @@ + + + diff --git a/Tithe-Vue/src/components/Forms/PersonForm.vue b/Tithe-Vue/src/components/Forms/PersonForm.vue new file mode 100644 index 0000000..15ee81a --- /dev/null +++ b/Tithe-Vue/src/components/Forms/PersonForm.vue @@ -0,0 +1,237 @@ + + + + + diff --git a/Tithe-Vue/src/components/MultiSelectBox/Multiselect.vue b/Tithe-Vue/src/components/MultiSelectBox/Multiselect.vue index bec7d03..7bb527d 100644 --- a/Tithe-Vue/src/components/MultiSelectBox/Multiselect.vue +++ b/Tithe-Vue/src/components/MultiSelectBox/Multiselect.vue @@ -259,9 +259,9 @@ :is-pointed="isPointed" :search="search" > - {{ localize(option[label]) }} + {{ localize(option[label]) }} {{ Object.values(option?.meta ?? {}).join(",") }} + >{{ Object.values(option?.meta ?? {}).join(", ") }} diff --git a/Tithe-Vue/src/components/SearchBoxes/EducationsMultiSelectBox.vue b/Tithe-Vue/src/components/SearchBoxes/EducationsMultiSelectBox.vue new file mode 100644 index 0000000..a9dbdb5 --- /dev/null +++ b/Tithe-Vue/src/components/SearchBoxes/EducationsMultiSelectBox.vue @@ -0,0 +1,100 @@ + + + diff --git a/Tithe-Vue/src/components/SearchBoxes/FamilyByParishSingleSelectBox.vue b/Tithe-Vue/src/components/SearchBoxes/FamilyByParishSingleSelectBox.vue new file mode 100644 index 0000000..86cbd0d --- /dev/null +++ b/Tithe-Vue/src/components/SearchBoxes/FamilyByParishSingleSelectBox.vue @@ -0,0 +1,87 @@ + + + diff --git a/Tithe-Vue/src/components/SearchBoxes/ForaneSingleSelectBox.vue b/Tithe-Vue/src/components/SearchBoxes/ForaneSingleSelectBox.vue index 841c70c..cf4bfce 100644 --- a/Tithe-Vue/src/components/SearchBoxes/ForaneSingleSelectBox.vue +++ b/Tithe-Vue/src/components/SearchBoxes/ForaneSingleSelectBox.vue @@ -1,5 +1,5 @@