Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 12bb4d4

Browse files
committed
feat: Add product activation functionality with corresponding command and controller endpoint
1 parent abc1d62 commit 12bb4d4

8 files changed

Lines changed: 55 additions & 12 deletions

File tree

src/main/java/com/zenfulcode/commercify/api/payment/PaymentAdminController.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.zenfulcode.commercify.api.payment.dto.response.CapturedPaymentResponse;
44
import com.zenfulcode.commercify.api.payment.mapper.PaymentDtoMapper;
5+
import com.zenfulcode.commercify.order.domain.valueobject.OrderId;
56
import com.zenfulcode.commercify.payment.application.command.CapturePaymentCommand;
67
import com.zenfulcode.commercify.payment.application.dto.CapturedPayment;
78
import com.zenfulcode.commercify.payment.application.service.PaymentApplicationService;
@@ -10,10 +11,7 @@
1011
import lombok.RequiredArgsConstructor;
1112
import org.springframework.http.ResponseEntity;
1213
import org.springframework.security.access.prepost.PreAuthorize;
13-
import org.springframework.web.bind.annotation.PathVariable;
14-
import org.springframework.web.bind.annotation.PostMapping;
15-
import org.springframework.web.bind.annotation.RequestMapping;
16-
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.bind.annotation.*;
1715

1816
@RestController
1917
@RequestMapping("/api/v2/payments/admin")
@@ -23,11 +21,11 @@ public class PaymentAdminController {
2321
private final PaymentApplicationService paymentService;
2422
private final PaymentDtoMapper paymentDtoMapper;
2523

26-
@PostMapping("/{paymentId}/capture")
24+
@PostMapping("/{orderId}/capture")
2725
public ResponseEntity<ApiResponse<CapturedPaymentResponse>> capturePayment(
28-
@PathVariable String paymentId) {
26+
@PathVariable String orderId) {
2927

30-
CapturePaymentCommand command = paymentDtoMapper.toCaptureCommand(PaymentId.of(paymentId));
28+
CapturePaymentCommand command = paymentDtoMapper.toCaptureCommand(OrderId.of(orderId));
3129
CapturedPayment capturedPayment = paymentService.capturePayment(command);
3230
CapturedPaymentResponse response = paymentDtoMapper.toCapturedResponse(capturedPayment);
3331

@@ -41,4 +39,5 @@ public ResponseEntity<ApiResponse<String>> refundPayment(
4139
// paymentService.refundPayment(OrderId.of(orderId));
4240
return ResponseEntity.ok(ApiResponse.success("Refund initiated"));
4341
}
42+
4443
}

src/main/java/com/zenfulcode/commercify/api/payment/mapper/PaymentDtoMapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.zenfulcode.commercify.api.payment.dto.response.PaymentResponse;
99
import com.zenfulcode.commercify.order.application.service.OrderApplicationService;
1010
import com.zenfulcode.commercify.order.domain.model.Order;
11+
import com.zenfulcode.commercify.order.domain.valueobject.OrderId;
1112
import com.zenfulcode.commercify.payment.application.command.CapturePaymentCommand;
1213
import com.zenfulcode.commercify.payment.application.command.InitiatePaymentCommand;
1314
import com.zenfulcode.commercify.payment.application.dto.CapturedPayment;
@@ -59,9 +60,9 @@ public WebhookPayload toWebhookPayload(WebhookRequest request) {
5960
}
6061
}
6162

62-
public CapturePaymentCommand toCaptureCommand(PaymentId paymentId) {
63+
public CapturePaymentCommand toCaptureCommand(OrderId orderId) {
6364
return new CapturePaymentCommand(
64-
paymentId,
65+
orderId,
6566
null
6667
);
6768
}

src/main/java/com/zenfulcode/commercify/api/product/ProductController.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.zenfulcode.commercify.product.domain.valueobject.ProductId;
1515
import com.zenfulcode.commercify.shared.interfaces.ApiResponse;
1616
import lombok.RequiredArgsConstructor;
17+
import lombok.extern.slf4j.Slf4j;
1718
import org.springframework.data.domain.Page;
1819
import org.springframework.data.domain.PageRequest;
1920
import org.springframework.data.domain.Sort;
@@ -22,6 +23,7 @@
2223
import org.springframework.validation.annotation.Validated;
2324
import org.springframework.web.bind.annotation.*;
2425

26+
@Slf4j
2527
@RestController
2628
@RequestMapping("/api/v2/products")
2729
@RequiredArgsConstructor
@@ -135,11 +137,24 @@ public ResponseEntity<ApiResponse<String>> updateVariantPrices(
135137
@PreAuthorize("hasRole('ADMIN')")
136138
public ResponseEntity<ApiResponse<String>> deactivateProduct(@PathVariable String productId) {
137139
DeactivateProductCommand command = new DeactivateProductCommand(ProductId.of(productId));
140+
log.info("Deactivating product with ID: {}", productId);
138141
productApplicationService.deactivateProduct(command);
139-
142+
log.info("Product deactivated successfully");
140143
return ResponseEntity.ok(ApiResponse.success("Product deactivated successfully"));
141144
}
142145

146+
@PostMapping("/{productId}/activate")
147+
@PreAuthorize("hasRole('ADMIN')")
148+
public ResponseEntity<ApiResponse<String>> activateProduct(@PathVariable String productId) {
149+
ActivateProductCommand command = new ActivateProductCommand(ProductId.of(productId));
150+
log.info("Activating product with ID: {}", productId);
151+
productApplicationService.activateProduct(command);
152+
153+
log.info("Product activated successfully");
154+
155+
return ResponseEntity.ok(ApiResponse.success("Product activated successfully"));
156+
}
157+
143158
@DeleteMapping("/{productId}")
144159
@PreAuthorize("hasRole('ADMIN')")
145160
public ResponseEntity<ApiResponse<String>> deleteProduct(@PathVariable String productId) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.zenfulcode.commercify.payment.application.command;
22

3+
import com.zenfulcode.commercify.order.domain.valueobject.OrderId;
34
import com.zenfulcode.commercify.payment.domain.valueobject.PaymentId;
45
import com.zenfulcode.commercify.shared.domain.model.Money;
56

67
public record CapturePaymentCommand(
7-
PaymentId paymentId,
8+
OrderId orderId,
89
Money captureAmount
910
) {
1011
}

src/main/java/com/zenfulcode/commercify/payment/application/service/PaymentApplicationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void handlePaymentCallback(PaymentProvider provider, WebhookPayload paylo
7979
// TODO: Make sure the capture currency is the same as the payment currency
8080
@Transactional
8181
public CapturedPayment capturePayment(CapturePaymentCommand command) {
82-
Payment payment = paymentDomainService.getPaymentById(command.paymentId());
82+
Payment payment = paymentDomainService.getPaymentByOrderId(command.orderId());
8383

8484
Money captureAmount = command.captureAmount() == null ? payment.getAmount() : command.captureAmount();
8585

src/main/java/com/zenfulcode/commercify/payment/domain/service/PaymentDomainService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.zenfulcode.commercify.payment.domain.service;
22

33
import com.zenfulcode.commercify.order.domain.model.Order;
4+
import com.zenfulcode.commercify.order.domain.valueobject.OrderId;
45
import com.zenfulcode.commercify.payment.domain.event.PaymentCreatedEvent;
56
import com.zenfulcode.commercify.payment.domain.exception.PaymentNotFoundException;
67
import com.zenfulcode.commercify.payment.domain.model.FailureReason;
@@ -126,4 +127,9 @@ public Payment getPaymentByProviderReference(String providerReference) {
126127
return paymentRepository.findByProviderReference(providerReference)
127128
.orElseThrow(() -> new PaymentNotFoundException(providerReference));
128129
}
130+
131+
public Payment getPaymentByOrderId(OrderId orderId) {
132+
return paymentRepository.findByOrderId(orderId)
133+
.orElseThrow(() -> new PaymentNotFoundException(orderId));
134+
}
129135
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.zenfulcode.commercify.product.application.command;
2+
3+
import com.zenfulcode.commercify.product.domain.valueobject.ProductId;
4+
5+
public record ActivateProductCommand(
6+
ProductId productId
7+
) {
8+
}

src/main/java/com/zenfulcode/commercify/product/application/service/ProductApplicationService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ public void deactivateProduct(DeactivateProductCommand command) {
131131
eventPublisher.publish(product.getDomainEvents());
132132
}
133133

134+
/**
135+
* Activates a product
136+
*/
137+
@Transactional
138+
public void activateProduct(ActivateProductCommand command) {
139+
Product product = productRepository.findById(command.productId())
140+
.orElseThrow(() -> new ProductNotFoundException(command.productId()));
141+
142+
product.activate();
143+
productRepository.save(product);
144+
eventPublisher.publish(product.getDomainEvents());
145+
}
146+
134147
/**
135148
* Deletes a product
136149
*/

0 commit comments

Comments
 (0)