Skip to content

Commit 3278965

Browse files
FINERACT-2243: Refactor payment type api for new concept;
1 parent 8d83b55 commit 3278965

File tree

40 files changed

+716
-451
lines changed

40 files changed

+716
-451
lines changed

fineract-core/dependencies.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ dependencies {
2424
// We also (normally should have) no need to ever use 'compileOnly'.
2525

2626
implementation(
27-
project(path: ':fineract-avro-schemas')
27+
project(path: ':fineract-avro-schemas'),
28+
project(path: ':fineract-command')
2829
)
2930

3031
// implementation dependencies are directly used (compiled against) in src/main (and src/test)

fineract-core/src/main/java/org/apache/fineract/portfolio/paymenttype/api/PaymentTypeApiResource.java

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
import io.swagger.v3.oas.annotations.media.Content;
2424
import io.swagger.v3.oas.annotations.media.Schema;
2525
import io.swagger.v3.oas.annotations.parameters.RequestBody;
26-
import io.swagger.v3.oas.annotations.responses.ApiResponse;
27-
import io.swagger.v3.oas.annotations.responses.ApiResponses;
2826
import io.swagger.v3.oas.annotations.tags.Tag;
27+
import jakarta.validation.Valid;
2928
import jakarta.ws.rs.Consumes;
3029
import jakarta.ws.rs.DELETE;
3130
import jakarta.ws.rs.GET;
@@ -37,17 +36,21 @@
3736
import jakarta.ws.rs.QueryParam;
3837
import jakarta.ws.rs.core.MediaType;
3938
import java.util.List;
39+
import java.util.UUID;
40+
import java.util.function.Supplier;
4041
import lombok.RequiredArgsConstructor;
41-
import org.apache.fineract.commands.domain.CommandWrapper;
42-
import org.apache.fineract.commands.service.CommandWrapperBuilder;
43-
import org.apache.fineract.commands.service.PortfolioCommandSourceWritePlatformService;
44-
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
45-
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
42+
import org.apache.fineract.command.core.CommandPipeline;
43+
import org.apache.fineract.infrastructure.core.service.DateUtils;
4644
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
47-
import org.apache.fineract.portfolio.paymenttype.data.PaymentTypeData;
48-
import org.apache.fineract.portfolio.paymenttype.data.request.PaymentTypeRequest;
45+
import org.apache.fineract.portfolio.paymenttype.command.CreatePaymentTypeCommand;
46+
import org.apache.fineract.portfolio.paymenttype.command.DeletePaymentTypeCommand;
47+
import org.apache.fineract.portfolio.paymenttype.command.UpdatePaymentTypeCommand;
48+
import org.apache.fineract.portfolio.paymenttype.component.PaymentTypeResponseMapperComponent;
49+
import org.apache.fineract.portfolio.paymenttype.data.CreatePaymentTypeRequest;
50+
import org.apache.fineract.portfolio.paymenttype.data.DeletePaymentTypeRequest;
51+
import org.apache.fineract.portfolio.paymenttype.data.PaymentTypeResponse;
52+
import org.apache.fineract.portfolio.paymenttype.data.UpdatePaymentTypeRequest;
4953
import org.apache.fineract.portfolio.paymenttype.domain.PaymentTypeRepositoryWrapper;
50-
import org.apache.fineract.portfolio.paymenttype.service.PaymentTypeReadPlatformService;
5154
import org.springframework.stereotype.Component;
5255

5356
@Path("/v1/paymenttypes")
@@ -57,84 +60,84 @@
5760
public class PaymentTypeApiResource {
5861

5962
private final PlatformSecurityContext securityContext;
60-
private final DefaultToApiJsonSerializer<PaymentTypeData> jsonSerializer;
61-
private final PaymentTypeReadPlatformService readPlatformService;
62-
private final PortfolioCommandSourceWritePlatformService commandWritePlatformService;
63-
private final PaymentTypeRepositoryWrapper paymentTypeRepositoryWrapper;
63+
private final PaymentTypeResponseMapperComponent paymentTypeResponseMapperComponent;
64+
private final CommandPipeline commandPipeline;
65+
private final PaymentTypeRepositoryWrapper paymentTypeRepository;
6466

6567
@GET
6668
@Consumes({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
6769
@Produces(MediaType.APPLICATION_JSON)
6870
@Operation(summary = "Retrieve all Payment Types", description = "Retrieve list of payment types")
69-
public List<PaymentTypeData> getAllPaymentTypes(
71+
public List<PaymentTypeResponse> getAllPaymentTypes(
7072
@QueryParam("onlyWithCode") @Parameter(description = "onlyWithCode") final boolean onlyWithCode) {
7173
securityContext.authenticatedUser().validateHasReadPermission(PaymentTypeApiResourceConstants.ENTITY_NAME);
72-
return onlyWithCode ? readPlatformService.retrieveAllPaymentTypesWithCode() : readPlatformService.retrieveAllPaymentTypes();
74+
return onlyWithCode ? paymentTypeResponseMapperComponent.retrieveAllPaymentTypesWithCode()
75+
: paymentTypeResponseMapperComponent.retrieveAllPaymentTypes();
7376
}
7477

7578
@GET
7679
@Path("{paymentTypeId}")
7780
@Consumes({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
7881
@Produces(MediaType.APPLICATION_JSON)
7982
@Operation(summary = "Retrieve a Payment Type", description = "Retrieves a payment type")
80-
public PaymentTypeData retrieveOnePaymentType(
83+
public PaymentTypeResponse retrieveOnePaymentType(
8184
@PathParam("paymentTypeId") @Parameter(description = "paymentTypeId") final Long paymentTypeId) {
8285
securityContext.authenticatedUser().validateHasReadPermission(PaymentTypeApiResourceConstants.ENTITY_NAME);
83-
paymentTypeRepositoryWrapper.findOneWithNotFoundDetection(paymentTypeId);
84-
return readPlatformService.retrieveOne(paymentTypeId);
86+
paymentTypeRepository.findOneWithNotFoundDetection(paymentTypeId);
87+
return paymentTypeResponseMapperComponent.retrieveOne(paymentTypeId);
8588
}
8689

8790
@POST
8891
@Consumes({ MediaType.APPLICATION_JSON })
8992
@Produces({ MediaType.APPLICATION_JSON })
9093
@Operation(summary = "Create a Payment Type", description = "Creates a new Payment type\n\n" + "Mandatory Fields: name\n\n"
9194
+ "Optional Fields: Description, isCashPayment,Position")
92-
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = PaymentTypeRequest.class)))
93-
@ApiResponses({
94-
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = PaymentTypeApiResourceSwagger.PostPaymentTypesResponse.class))) })
95-
public CommandProcessingResult createPaymentType(@Parameter(hidden = true) PaymentTypeRequest paymentTypeRequest) {
95+
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = CreatePaymentTypeRequest.class)))
96+
public PaymentTypeResponse createPaymentType(@Parameter(hidden = true) @Valid CreatePaymentTypeRequest paymentTypeRequest) {
97+
var commandPaymentType = new CreatePaymentTypeCommand();
98+
commandPaymentType.setId(UUID.randomUUID());
99+
commandPaymentType.setCreatedAt(DateUtils.getAuditOffsetDateTime());
100+
commandPaymentType.setPayload(paymentTypeRequest);
96101

97-
final CommandWrapper commandRequest = new CommandWrapperBuilder().createPaymentType()
98-
.withJson(jsonSerializer.serialize(paymentTypeRequest)).build();
102+
Supplier<PaymentTypeResponse> result = commandPipeline.send(commandPaymentType);
99103

100-
CommandProcessingResult result = commandWritePlatformService.logCommandSource(commandRequest);
101-
return result;
104+
return result.get();
102105
}
103106

104107
@PUT
105108
@Path("{paymentTypeId}")
106109
@Consumes({ MediaType.APPLICATION_JSON })
107110
@Produces({ MediaType.APPLICATION_JSON })
108111
@Operation(summary = "Update a Payment Type", description = "Updates a Payment Type")
109-
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = PaymentTypeApiResourceSwagger.PutPaymentTypesPaymentTypeIdRequest.class)))
110-
@ApiResponses({
111-
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = PaymentTypeApiResourceSwagger.PutPaymentTypesPaymentTypeIdResponse.class))) })
112-
public CommandProcessingResult updatePaymentType(
112+
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = UpdatePaymentTypeRequest.class)))
113+
public PaymentTypeResponse updatePaymentType(
113114
@PathParam("paymentTypeId") @Parameter(description = "paymentTypeId") final Long paymentTypeId,
114-
@Parameter(hidden = true) final String apiRequestBodyAsJson) {
115+
@Parameter(hidden = true) @Valid UpdatePaymentTypeRequest paymentTypeRequest) {
115116

116-
final CommandWrapper commandRequest = new CommandWrapperBuilder().updatePaymentType(paymentTypeId).withJson(apiRequestBodyAsJson)
117-
.build();
117+
var commandPaymentType = new UpdatePaymentTypeCommand();
118+
commandPaymentType.setId(UUID.randomUUID());
119+
commandPaymentType.setCreatedAt(DateUtils.getAuditOffsetDateTime());
120+
commandPaymentType.setPayload(paymentTypeRequest.setId(paymentTypeId));
118121

119-
final CommandProcessingResult result = commandWritePlatformService.logCommandSource(commandRequest);
120-
return result;
122+
Supplier<PaymentTypeResponse> result = commandPipeline.send(commandPaymentType);
123+
124+
return result.get();
121125
}
122126

123127
@DELETE
124128
@Path("{paymentTypeId}")
125129
@Consumes({ MediaType.APPLICATION_JSON })
126130
@Produces({ MediaType.APPLICATION_JSON })
127131
@Operation(summary = "Delete a Payment Type", description = "Deletes payment type")
128-
@ApiResponses({
129-
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = PaymentTypeApiResourceSwagger.DeletePaymentTypesPaymentTypeIdResponse.class))) })
130-
public CommandProcessingResult deleteCode(
131-
@PathParam("paymentTypeId") @Parameter(description = "paymentTypeId") final Long paymentTypeId) {
132-
133-
final CommandWrapper commandRequest = new CommandWrapperBuilder().deletePaymentType(paymentTypeId).build();
132+
public PaymentTypeResponse deleteCode(@PathParam("paymentTypeId") @Parameter(description = "paymentTypeId") final Long paymentTypeId) {
133+
var commandPaymentType = new DeletePaymentTypeCommand();
134+
commandPaymentType.setId(UUID.randomUUID());
135+
commandPaymentType.setCreatedAt(DateUtils.getAuditOffsetDateTime());
136+
commandPaymentType.setPayload(new DeletePaymentTypeRequest(paymentTypeId));
134137

135-
final CommandProcessingResult result = commandWritePlatformService.logCommandSource(commandRequest);
138+
final Supplier<PaymentTypeResponse> result = commandPipeline.send(commandPaymentType);
136139

137-
return result;
140+
return result.get();
138141
}
139142

140143
}

fineract-core/src/main/java/org/apache/fineract/portfolio/paymenttype/api/PaymentTypeApiResourceConstants.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
*/
1919
package org.apache.fineract.portfolio.paymenttype.api;
2020

21-
import java.util.Arrays;
22-
import java.util.HashSet;
23-
import java.util.Set;
24-
2521
public final class PaymentTypeApiResourceConstants {
2622

2723
private PaymentTypeApiResourceConstants() {
@@ -38,6 +34,4 @@ private PaymentTypeApiResourceConstants() {
3834
public static final String POSITION = "position";
3935
public static final String CODE_NAME = "code_name";
4036
public static final String IS_SYSTEM_DEFINED = "system_defined";
41-
42-
static final Set<String> RESPONSE_DATA_PARAMETERS = new HashSet<>(Arrays.asList(ID, NAME, DESCRIPTION, ISCASHPAYMENT));
4337
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.portfolio.paymenttype.command;
20+
21+
import lombok.Data;
22+
import lombok.EqualsAndHashCode;
23+
import org.apache.fineract.command.core.Command;
24+
import org.apache.fineract.portfolio.paymenttype.data.CreatePaymentTypeRequest;
25+
26+
@Data
27+
@EqualsAndHashCode(callSuper = true)
28+
public class CreatePaymentTypeCommand extends Command<CreatePaymentTypeRequest> {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.portfolio.paymenttype.command;
20+
21+
import lombok.Data;
22+
import lombok.EqualsAndHashCode;
23+
import org.apache.fineract.command.core.Command;
24+
import org.apache.fineract.portfolio.paymenttype.data.DeletePaymentTypeRequest;
25+
26+
@Data
27+
@EqualsAndHashCode(callSuper = true)
28+
public class DeletePaymentTypeCommand extends Command<DeletePaymentTypeRequest> {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.portfolio.paymenttype.command;
20+
21+
import lombok.Data;
22+
import lombok.EqualsAndHashCode;
23+
import org.apache.fineract.command.core.Command;
24+
import org.apache.fineract.portfolio.paymenttype.data.UpdatePaymentTypeRequest;
25+
26+
@Data
27+
@EqualsAndHashCode(callSuper = true)
28+
public class UpdatePaymentTypeCommand extends Command<UpdatePaymentTypeRequest> {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.portfolio.paymenttype.component;
20+
21+
import java.util.List;
22+
import lombok.RequiredArgsConstructor;
23+
import org.apache.fineract.portfolio.paymenttype.data.PaymentTypeResponse;
24+
import org.apache.fineract.portfolio.paymenttype.mapper.PaymentTypeMapper;
25+
import org.apache.fineract.portfolio.paymenttype.service.PaymentTypeReadPlatformService;
26+
import org.springframework.stereotype.Component;
27+
28+
@Component
29+
@RequiredArgsConstructor
30+
public class PaymentTypeResponseMapperComponent {
31+
32+
private final PaymentTypeReadPlatformService paymentTypeReadPlatformService;
33+
private final PaymentTypeMapper paymentTypeMapper;
34+
35+
public List<PaymentTypeResponse> retrieveAllPaymentTypes() {
36+
return paymentTypeReadPlatformService.retrieveAllPaymentTypes().stream().map(paymentTypeMapper::map).toList();
37+
}
38+
39+
public List<PaymentTypeResponse> retrieveAllPaymentTypesWithCode() {
40+
return paymentTypeReadPlatformService.retrieveAllPaymentTypesWithCode().stream().map(paymentTypeMapper::map).toList();
41+
}
42+
43+
public PaymentTypeResponse retrieveOne(Long paymentTypeId) {
44+
return paymentTypeMapper.map(paymentTypeReadPlatformService.retrieveOne(paymentTypeId));
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.portfolio.paymenttype.data;
20+
21+
import com.fasterxml.jackson.annotation.JsonProperty;
22+
import jakarta.validation.constraints.PositiveOrZero;
23+
import jakarta.validation.constraints.Size;
24+
import java.io.Serial;
25+
import java.io.Serializable;
26+
import lombok.AllArgsConstructor;
27+
import lombok.Builder;
28+
import lombok.Data;
29+
import lombok.NoArgsConstructor;
30+
import lombok.experimental.Accessors;
31+
import lombok.experimental.FieldNameConstants;
32+
import org.apache.fineract.portfolio.paymenttype.validation.NotEmptyIfPresent;
33+
34+
@Builder
35+
@Data
36+
@NoArgsConstructor
37+
@AllArgsConstructor
38+
@FieldNameConstants
39+
@Accessors(chain = true)
40+
public class CreatePaymentTypeRequest implements Serializable {
41+
42+
@Serial
43+
private static final long serialVersionUID = 1L;
44+
45+
private Long id;
46+
@NotEmptyIfPresent
47+
private String name;
48+
49+
@Size(max = 500, message = "{org.apache.fineract.portfolio.paymenttype.data.payment-type-response.description.length.max}")
50+
private String description;
51+
private Boolean isCashPayment;
52+
53+
@PositiveOrZero(message = "{org.apache.fineract.portfolio.paymenttype.data.payment-type-response.position.positive}")
54+
private Integer position;
55+
56+
@JsonProperty("code_name")
57+
@Size(max = 100, message = "{org.apache.fineract.portfolio.paymenttype.data.payment-type-response.codeName.length.max}")
58+
private String codeName;
59+
60+
@JsonProperty("system_defined")
61+
private Boolean isSystemDefined;
62+
}

0 commit comments

Comments
 (0)