Skip to content

Commit

Permalink
added customer api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
harmlessprince committed Feb 16, 2025
1 parent 0c7d9ec commit 720f3ab
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
public class CustomerController {
private final AuthenticationService authenticationService;
private final CustomerService customerService;
private final UserService userService;
private final UserMapper userMapper;
private final ObjectMapper mapper;

Expand All @@ -67,13 +68,34 @@ public ResponseEntity<CustomResponse<PaginatedResponse<CustomerResponse>>> getAl
@ShopOwnerAccess
public ResponseEntity<CustomResponse<CustomerResponse>> fetchCustomer(@PathVariable() String customerId) {
Optional<User> customer = this.customerService.findById(customerId);
if(customer.isEmpty()) {
if (customer.isEmpty()) {
throw new CustomResourceNotFoundException("Customer not found");
}
CustomerResponse customResponse = mapper.convertValue(customer, CustomerResponse.class);
return ResponseEntity.ok(CustomResponse.sendSuccessResponse(mapper.convertValue(customer, CustomerResponse.class), "Customer retrieved successfully"));
}

@PatchMapping("/{customerId}/deactivate")
@ShopOwnerAccess
public ResponseEntity<CustomResponse<CustomerResponse>> deactivateCustomer(@PathVariable() String customerId) {
Optional<User> customer = this.customerService.findById(customerId);
if (customer.isEmpty()) {
throw new CustomResourceNotFoundException("Customer not found");
}
this.userService.deactivateUser(customer.get());
return ResponseEntity.ok(CustomResponse.sendSuccessResponse(null, "Customer deactivated successfully"));
}

@PatchMapping("/{customerId}/activiate")
@ShopOwnerAccess
public ResponseEntity<CustomResponse<CustomerResponse>> activateCustomer(@PathVariable() String customerId) {
Optional<User> customer = this.customerService.findById(customerId);
if (customer.isEmpty()) {
throw new CustomResourceNotFoundException("Customer not found");
}
this.userService.activateUser(customer.get());
return ResponseEntity.ok(CustomResponse.sendSuccessResponse(null, "Customer activated successfully"));
}


@PostMapping("/signup")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ public boolean isCredentialsNonExpired() {

@Override
public boolean isEnabled() {
return true;
return status == null || status.equals("active");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.harmlessprince.ecommerceApi.user;

import com.harmlessprince.ecommerceApi.exceptions.CustomBadRequestException;
import com.harmlessprince.ecommerceApi.exceptions.CustomResourceNotFoundException;
import com.harmlessprince.ecommerceApi.permission.Permission;
import com.harmlessprince.ecommerceApi.role.Role;
Expand Down Expand Up @@ -77,10 +78,7 @@ public Collection<? extends GrantedAuthority> getAuthorities(Role role) {

authorities.add(new SimpleGrantedAuthority(userRole));
if (fetchedRole.getPermissions() != null) {
List<String> permissions = fetchedRole.getPermissions()
.stream()
.map(Permission::getSlug)
.toList();
List<String> permissions = fetchedRole.getPermissions().stream().map(Permission::getSlug).toList();
permissions.forEach(permission -> authorities.add(new SimpleGrantedAuthority(permission)));
}
// Print authorities for debugging purposes
Expand All @@ -99,4 +97,20 @@ public void assignTenantToUser(User user, Tenant tenant) {
user.setTenantId(tenant.getId());
userRepository.save(user);
}

public void deactivateUser(User user) {
if (Objects.equals(user.getStatus(), "inactive")) {
throw new CustomBadRequestException("User has already been deactivated");
}
user.setStatus("inactive");
userRepository.save(user);
}

public void activateUser(User user) {
if (Objects.equals(user.getStatus(), "active")) {
throw new CustomBadRequestException("User has already been activated");
}
user.setStatus("active");
userRepository.save(user);
}
}

0 comments on commit 720f3ab

Please sign in to comment.