Skip to content
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'com.flexcodelabs'
version = '0.0.48'
version = '0.0.49'
description = 'Flextuma App'

java {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.flexcodelabs.flextuma.core.dtos;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;

public record ProfileUpdateDto(
@NotBlank String name,
@NotBlank String username,
@Email String email,
@NotBlank String phoneNumber) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
}

List<Predicate> predicates = new ArrayList<>();

predicates.add(cb.equal(root.get(CREATED_BY), currentUser));

Organisation organisation = currentUser.getOrganisation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.flexcodelabs.flextuma.core.dtos.LoginDto;
import com.flexcodelabs.flextuma.core.dtos.RegisterDto;
import com.flexcodelabs.flextuma.core.dtos.ProfileUpdateDto;
import com.flexcodelabs.flextuma.core.dtos.PasswordChangeDto;
import com.flexcodelabs.flextuma.core.dto.ApiResponse;
import com.flexcodelabs.flextuma.core.dtos.UserResponseDto;
Expand Down Expand Up @@ -136,6 +138,15 @@ public ResponseEntity<Object> me() {
.body(user);
}

@PatchMapping("/me")
public ResponseEntity<Object> updateProfile(@Valid @RequestBody ProfileUpdateDto request) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null || !auth.isAuthenticated() || auth instanceof AnonymousAuthenticationToken) {
return ResponseEntity.status(401).body(ErrorResponse.unauthorized("Unauthorized"));
}
return ResponseEntity.ok(userService.updateProfile(auth.getName(), request));
}

@PostMapping("/verify")
public ResponseEntity<Object> verify(@Valid @RequestBody VerificationRequestDto request,
HttpServletRequest httpRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.flexcodelabs.flextuma.core.entities.auth.User;
import com.flexcodelabs.flextuma.core.dtos.RegisterDto;
import com.flexcodelabs.flextuma.core.dtos.ProfileUpdateDto;
import com.flexcodelabs.flextuma.core.repositories.UserRepository;
import com.flexcodelabs.flextuma.core.services.BaseService;

Expand Down Expand Up @@ -151,6 +152,25 @@ public User register(RegisterDto request) {
return repository.save(user);
}

@org.springframework.transaction.annotation.Transactional
public User updateProfile(String currentUsername, ProfileUpdateDto request) {
User user = findByUsername(currentUsername);
repository.findByUsername(request.username())
.filter(existing -> !existing.getId().equals(user.getId()))
.ifPresent(existing -> { throw new ResponseStatusException(HttpStatus.CONFLICT, "Username already exists"); });
repository.findByEmail(request.email())
.filter(existing -> !existing.getId().equals(user.getId()))
.ifPresent(existing -> { throw new ResponseStatusException(HttpStatus.CONFLICT, "Email already exists"); });
repository.findByPhoneNumber(request.phoneNumber())
.filter(existing -> !existing.getId().equals(user.getId()))
.ifPresent(existing -> { throw new ResponseStatusException(HttpStatus.CONFLICT, "Phone number already exists"); });
user.setName(request.name());
user.setUsername(request.username());
user.setEmail(request.email());
user.setPhoneNumber(request.phoneNumber());
return repository.save(user);
}

public User changePassword(User user, String newPassword) {
User managedUser = repository.findById(user.getId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ protected void validateDelete(ConnectorConfig entity) {
@Override
public ConnectorConfig update(UUID id, ConnectorConfig entity) {
checkPermission(getUpdatePermission());
ConnectorConfig existing = getRepository().findById(id)
.orElseThrow(() -> new org.springframework.web.server.ResponseStatusException(
org.springframework.http.HttpStatus.NOT_FOUND, getEntitySingular() + " not found"));
ConnectorConfig existing = findAccessibleById(id);

if (entity.getTenantId() != null && entity.getTenantId().contains("****")) {
entity.setTenantId(existing.getTenantId());
Expand All @@ -103,4 +101,4 @@ public ConnectorConfig update(UUID id, ConnectorConfig entity) {

return super.update(id, entity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ protected void validateDelete(Contact entity) {
public java.util.Map<String, String> delete(UUID id) {
checkPermission(getDeletePermission());

Contact entity = getRepository().findById(id)
.orElseThrow(() -> new org.springframework.web.server.ResponseStatusException(
org.springframework.http.HttpStatus.NOT_FOUND, getEntitySingular() + " not found"));
Contact entity = findAccessibleById(id);

validateDelete(entity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public class WhatsAppWebhookConfigService extends BaseService<WhatsAppWebhookCon
@Value("${flextuma.public-base-url:}") private String publicBaseUrl;
protected JpaRepository<WhatsAppWebhookConfig, UUID> getRepository() { return repository; }
protected JpaSpecificationExecutor<WhatsAppWebhookConfig> getRepositoryAsExecutor() { return repository; }
protected String getReadPermission() { return WhatsAppWebhookConfig.READ; }
protected String getAddPermission() { return WhatsAppWebhookConfig.ADD; }
protected String getUpdatePermission() { return WhatsAppWebhookConfig.UPDATE; }
protected String getDeletePermission() { return WhatsAppWebhookConfig.DELETE; }
// Each configuration is tenant-scoped by BaseService, so every signed-in
// user can manage only their own WhatsApp webhook configurations.
protected String getReadPermission() { return "ALL"; }
protected String getAddPermission() { return "ALL"; }
protected String getUpdatePermission() { return "ALL"; }
protected String getDeletePermission() { return "ALL"; }
public String getEntityPlural() { return WhatsAppWebhookConfig.NAME_PLURAL; }
protected String getEntitySingular() { return WhatsAppWebhookConfig.NAME_SINGULAR; }
public String getPropertyName() { return WhatsAppWebhookConfig.PLURAL; }
Expand Down