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

[SELC-5827] Feat: added custom codec for user date #201

Merged
merged 4 commits into from
Oct 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import org.openapi.quarkus.user_registry_json.api.UserApi;
import org.openapi.quarkus.user_registry_json.model.UserResource;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;

import static it.pagopa.selfcare.user.event.UserInstitutionCdcService.USERS_FIELD_LIST_WITHOUT_FISCAL_CODE;
Expand Down Expand Up @@ -79,8 +82,8 @@ OnboardedProduct dummyOnboardedProduct(String productRole, OnboardedProductState
OnboardedProduct onboardedProduct = new OnboardedProduct();
onboardedProduct.setProductId("productId");
onboardedProduct.setProductRole(productRole);
onboardedProduct.setCreatedAt(LocalDateTime.of(2024,1,day,0,0,0));
onboardedProduct.setUpdatedAt(LocalDateTime.of(2024,1,day,0,0,0));
onboardedProduct.setCreatedAt(OffsetDateTime.of(2024, 1, day, 0, 0, 0, 0, ZoneOffset.UTC));
onboardedProduct.setUpdatedAt(OffsetDateTime.of(2024, 1, day, 0, 0, 0, 0, ZoneOffset.UTC));
onboardedProduct.setStatus(state);
return onboardedProduct;
}
Expand Down
20 changes: 10 additions & 10 deletions apps/user-ms/src/main/docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"in" : "query",
"required" : true,
"schema" : {
"$ref" : "#/components/schemas/LocalDateTime"
"$ref" : "#/components/schemas/OffsetDateTime"
}
}, {
"name" : "institutionId",
Expand Down Expand Up @@ -187,7 +187,7 @@
"in" : "query",
"required" : true,
"schema" : {
"$ref" : "#/components/schemas/LocalDateTime"
"$ref" : "#/components/schemas/OffsetDateTime"
}
}, {
"name" : "userIds",
Expand Down Expand Up @@ -1388,10 +1388,10 @@
}
}
},
"LocalDateTime" : {
"OffsetDateTime" : {
"format" : "date-time",
"type" : "string",
"example" : "2022-03-10T12:15:50"
"example" : "2022-03-10T12:15:50-04:00"
},
"OnboardedProductResponse" : {
"type" : "object",
Expand All @@ -1416,10 +1416,10 @@
"$ref" : "#/components/schemas/Env"
},
"createdAt" : {
"$ref" : "#/components/schemas/LocalDateTime"
"$ref" : "#/components/schemas/OffsetDateTime"
},
"updatedAt" : {
"$ref" : "#/components/schemas/LocalDateTime"
"$ref" : "#/components/schemas/OffsetDateTime"
}
}
},
Expand Down Expand Up @@ -1450,10 +1450,10 @@
"$ref" : "#/components/schemas/Env"
},
"createdAt" : {
"$ref" : "#/components/schemas/LocalDateTime"
"$ref" : "#/components/schemas/OffsetDateTime"
},
"updatedAt" : {
"$ref" : "#/components/schemas/LocalDateTime"
"$ref" : "#/components/schemas/OffsetDateTime"
},
"delegationId" : {
"type" : "string"
Expand Down Expand Up @@ -1780,10 +1780,10 @@
"type" : "string"
},
"createdAt" : {
"$ref" : "#/components/schemas/LocalDateTime"
"$ref" : "#/components/schemas/OffsetDateTime"
},
"updatedAt" : {
"$ref" : "#/components/schemas/LocalDateTime"
"$ref" : "#/components/schemas/OffsetDateTime"
},
"eventType" : {
"$ref" : "#/components/schemas/QueueEvent"
Expand Down
20 changes: 10 additions & 10 deletions apps/user-ms/src/main/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ paths:
in: query
required: true
schema:
$ref: "#/components/schemas/LocalDateTime"
$ref: "#/components/schemas/OffsetDateTime"
- name: institutionId
in: query
schema:
Expand Down Expand Up @@ -142,7 +142,7 @@ paths:
in: query
required: true
schema:
$ref: "#/components/schemas/LocalDateTime"
$ref: "#/components/schemas/OffsetDateTime"
- name: userIds
in: query
required: true
Expand Down Expand Up @@ -998,10 +998,10 @@ components:
type: string
reason:
type: string
LocalDateTime:
OffsetDateTime:
format: date-time
type: string
example: 2022-03-10T12:15:50
example: 2022-03-10T12:15:50-04:00
OnboardedProductResponse:
type: object
properties:
Expand All @@ -1020,9 +1020,9 @@ components:
env:
$ref: "#/components/schemas/Env"
createdAt:
$ref: "#/components/schemas/LocalDateTime"
$ref: "#/components/schemas/OffsetDateTime"
updatedAt:
$ref: "#/components/schemas/LocalDateTime"
$ref: "#/components/schemas/OffsetDateTime"
OnboardedProductState:
enum:
- ACTIVE
Expand Down Expand Up @@ -1050,9 +1050,9 @@ components:
env:
$ref: "#/components/schemas/Env"
createdAt:
$ref: "#/components/schemas/LocalDateTime"
$ref: "#/components/schemas/OffsetDateTime"
updatedAt:
$ref: "#/components/schemas/LocalDateTime"
$ref: "#/components/schemas/OffsetDateTime"
delegationId:
type: string
userProductActions:
Expand Down Expand Up @@ -1289,9 +1289,9 @@ components:
onboardingTokenId:
type: string
createdAt:
$ref: "#/components/schemas/LocalDateTime"
$ref: "#/components/schemas/OffsetDateTime"
updatedAt:
$ref: "#/components/schemas/LocalDateTime"
$ref: "#/components/schemas/OffsetDateTime"
eventType:
$ref: "#/components/schemas/QueueEvent"
user:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package it.pagopa.selfcare.user.conf;

import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;

public class DateCodec implements Codec<OffsetDateTime> {

@Override
public OffsetDateTime decode(BsonReader reader, DecoderContext decoderContext) {
BsonType currentType = reader.getCurrentBsonType();
if (currentType.equals(BsonType.DATE_TIME)) {
return OffsetDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneId.systemDefault());
}
return null;
}


@Override
public void encode(BsonWriter bsonWriter, OffsetDateTime aLong, EncoderContext encoderContext) {
bsonWriter.writeDateTime(aLong.toInstant().toEpochMilli());
}

@Override
public Class<OffsetDateTime> getEncoderClass() {
return OffsetDateTime.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.pagopa.selfcare.user.conf;

import com.mongodb.MongoClientSettings;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;

@ApplicationScoped
public class MongoConfig {

@Produces
public CodecRegistry produceCodecRegistry() {
CodecRegistry defaultCodecRegistry = MongoClientSettings.getDefaultCodecRegistry();
CodecRegistry customCodecRegistry = CodecRegistries.fromCodecs(new DateCodec());

return CodecRegistries.fromRegistries(customCodecRegistry, defaultCodecRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;

@Authenticated
@Tag(name = "Events")
Expand All @@ -30,7 +30,7 @@ public class EventsController {
@Path(value = "/sc-users")
public Uni<Void> sendUsersEvents(@QueryParam(value = "institutionId") String institutionId,
@QueryParam(value = "userId") String userId,
@NotNull @QueryParam(value = "fromDate") LocalDateTime fromDate){
@NotNull @QueryParam(value = "fromDate") OffsetDateTime fromDate){
return userService.sendEventsByDateAndUserIdAndInstitutionId(fromDate, institutionId, userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.openapi.quarkus.user_registry_json.model.Problem;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;

@Authenticated
Expand Down Expand Up @@ -82,7 +82,7 @@ public Multi<UserInstitutionResponse> retrieveUserInstitutions(@PathParam(value
public Uni<Response> updateUserProductCreatedAt(@PathParam(value = "institutionId") String institutionId,
@PathParam(value = "productId") String productId,
@NotNull @QueryParam(value = "userIds") List<String> userIds,
@NotNull @QueryParam(value = "createdAt") LocalDateTime createdAt) {
@NotNull @QueryParam(value = "createdAt") OffsetDateTime createdAt) {
return userService.updateUserProductCreatedAt(institutionId, userIds, productId, createdAt)
.map(ignore -> Response
.status(HttpStatus.SC_NO_CONTENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.Data;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;

@Data
public class OnboardedProductResponse {
Expand All @@ -17,7 +17,7 @@ public class OnboardedProductResponse {
@Schema(description = "Available values: MANAGER, DELEGATE, SUB_DELEGATE, OPERATOR, ADMIN_EA")
private String role;
private Env env;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private OffsetDateTime createdAt;
private OffsetDateTime updatedAt;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import it.pagopa.selfcare.user.model.constants.QueueEvent;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;

/**
* This objects wrap user's info sent on topic sc-users
Expand All @@ -17,8 +17,8 @@ public class UserNotificationResponse {
private String institutionId;
private String productId;
private String onboardingTokenId;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private OffsetDateTime createdAt;
private OffsetDateTime updatedAt;
private QueueEvent eventType;
private UserToNotify user;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import lombok.experimental.FieldNameConstants;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;

import static it.pagopa.selfcare.onboarding.common.Env.ROOT;
Expand All @@ -23,8 +23,8 @@ public class OnboardedProductWithActions {
@Schema(description = "Available values: MANAGER, DELEGATE, SUB_DELEGATE, OPERATOR, ADMIN_EA")
private String role;
private Env env = ROOT;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private OffsetDateTime createdAt;
private OffsetDateTime updatedAt;
private String delegationId;
private List<String> userProductActions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public interface OnboardedProductMapper {

@Mapping(target = "status", expression = "java(it.pagopa.selfcare.user.model.constants.OnboardedProductState.ACTIVE)")
@Mapping(target = "env", expression = "java(it.pagopa.selfcare.onboarding.common.Env.ROOT)")
@Mapping(target = "createdAt", expression = "java(java.time.LocalDateTime.now())")
@Mapping(target = "updatedAt", expression = "java(java.time.LocalDateTime.now())")
@Mapping(target = "createdAt", expression = "java(java.time.OffsetDateTime.now())")
@Mapping(target = "updatedAt", expression = "java(java.time.OffsetDateTime.now())")
@Mapping(target = "productRole", source = "productRole")
OnboardedProduct toNewOnboardedProduct(CreateUserDto.Product product, String productRole);

@Mapping(target = "status", expression = "java(it.pagopa.selfcare.user.model.constants.OnboardedProductState.ACTIVE)")
@Mapping(target = "env", expression = "java(it.pagopa.selfcare.onboarding.common.Env.ROOT)")
@Mapping(target = "createdAt", expression = "java(java.time.LocalDateTime.now())")
@Mapping(target = "updatedAt", expression = "java(java.time.LocalDateTime.now())")
@Mapping(target = "createdAt", expression = "java(java.time.OffsetDateTime.now())")
@Mapping(target = "updatedAt", expression = "java(java.time.OffsetDateTime.now())")
@Mapping(target = "productRole", source = "productRole")
OnboardedProduct toNewOnboardedProduct(AddUserRoleDto.Product product, String productRole);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.mapstruct.Named;
import software.amazon.awssdk.utils.CollectionUtils;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -88,7 +88,7 @@ default List<OnboardedProduct> toNewOnboardedProductFromAddUserRole(AddUserRoleD
}

default OnboardedProduct buildOnboardedProduct(){
LocalDateTime now = java.time.LocalDateTime.now();
OffsetDateTime now = java.time.OffsetDateTime.now();
OnboardedProduct onboardedProduct = new OnboardedProduct();
onboardedProduct.setStatus(OnboardedProductState.ACTIVE);
onboardedProduct.setEnv(it.pagopa.selfcare.onboarding.common.Env.ROOT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import it.pagopa.selfcare.user.entity.UserInstitution;
import it.pagopa.selfcare.user.model.constants.OnboardedProductState;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;

Expand All @@ -25,19 +25,19 @@ public interface UserInstitutionService {

Multi<UserInstitution> findAllWithFilter(Map<String, Object> queryParameter);

Multi<UserInstitution> findUserInstitutionsAfterDateWithFilter(Map<String,Object> queryParameter, LocalDateTime fromDate);
Multi<UserInstitution> findUserInstitutionsAfterDateWithFilter(Map<String,Object> queryParameter, OffsetDateTime fromDate);

Uni<UserInstitution> retrieveFirstFilteredUserInstitution(Map<String, Object> queryParameter);

Uni<Long> deleteUserInstitutionProduct(String userId, String institutionId, String productId);

Uni<Long> updateUserStatusWithOptionalFilterByInstitutionAndProduct(String userId, String institutionId, String productId, PartyRole role, String productRole, OnboardedProductState status);

Uni<Long> updateUserCreatedAtByInstitutionAndProduct(String institutionId, List<String> userIds, String productId, LocalDateTime createdAt);
Uni<Long> updateUserCreatedAtByInstitutionAndProduct(String institutionId, List<String> userIds, String productId, OffsetDateTime createdAt);

Multi<UserInstitution> findUserInstitutionsAfterDateWithFilter(Map<String, Object> queryParameter, LocalDateTime fromDate, Integer page);
Multi<UserInstitution> findUserInstitutionsAfterDateWithFilter(Map<String, Object> queryParameter, OffsetDateTime fromDate, Integer page);

Uni<Integer> pageCountUserInstitutionsAfterDateWithFilter(Map<String, Object> queryParameter, LocalDateTime fromDate);
Uni<Integer> pageCountUserInstitutionsAfterDateWithFilter(Map<String, Object> queryParameter, OffsetDateTime fromDate);

Uni<List<UserInstitution>> retrieveFilteredUserInstitution(Map<String, Object> queryParameter);

Expand Down
Loading
Loading