Skip to content

Commit

Permalink
feature: Refine wallet, payment component
Browse files Browse the repository at this point in the history
  • Loading branch information
Tran Gia Bao committed Feb 15, 2025
1 parent f6d2013 commit 922d111
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

import java.util.UUID;


public interface PaymentRepository extends AbstractBaseRepository<PaymentEntity> {

@Query("""
SELECT p
FROM PaymentEntity p
"""
)
Page<PaymentEntity> findByName(String name, Pageable pageable);
Page<PaymentEntity> findByEnterpriseId(UUID enterpriseId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
import commons.springfw.impl.repositories.AbstractBaseRepository;
import enterprise.entities.WalletEntity;

import java.util.Optional;
import java.util.UUID;

public interface WalletRepository extends AbstractBaseRepository<WalletEntity> {
Optional<WalletEntity> findByEnterpriseId(UUID enterpriseId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import enterprise.services.PaymentService;
import green_buildings.commons.api.dto.SearchCriteriaDTO;
import green_buildings.commons.api.dto.SearchResultDTO;
import green_buildings.commons.api.security.UserRole;
import jakarta.annotation.security.RolesAllowed;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -24,6 +26,7 @@ public class PaymentController {
private final PaymentMapper paymentMapper;

@PostMapping("/search")
@RolesAllowed(UserRole.RoleNameConstant.ENTERPRISE_OWNER)
public ResponseEntity<SearchResultDTO<PaymentDTO>> searchPayment(@RequestBody SearchCriteriaDTO<PaymentCriteriaDTO> searchCriteria) {
var pageable = CommonMapper.toPageable(searchCriteria.page(), searchCriteria.sort());
var searchResults = paymentService.search(searchCriteria, pageable);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package enterprise.rest;

import commons.springfw.impl.securities.UserContextData;
import green_buildings.commons.api.exceptions.BusinessException;
import enterprise.services.WalletService;
import green_buildings.commons.api.security.UserRole;
import jakarta.annotation.security.RolesAllowed;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Objects;

@RestController
@RequestMapping("/api/wallet")
@RequiredArgsConstructor
public class WalletController {

private final WalletService walletService;

@GetMapping("/balance")
public ResponseEntity<Void> getBalance(@AuthenticationPrincipal UserContextData userContextData) {
//TODO: Must Authentication => get Id Enterpirse => get Wallet => get Balence
if (Objects.nonNull(userContextData.getEnterpriseId())) {
throw new BusinessException(StringUtils.EMPTY, "error.user.already.belongs.to.enterprise", Collections.emptyList());
}
return ResponseEntity.ok().build();
@RolesAllowed(UserRole.RoleNameConstant.ENTERPRISE_OWNER)
public ResponseEntity<Long> getBalance(@AuthenticationPrincipal UserContextData userContextData) {
return ResponseEntity.ok().body(walletService.getBalance());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package enterprise.services;

import enterprise.entities.WalletEntity;

public interface WalletService {
WalletEntity getBalance();
Long getBalance();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
Expand All @@ -26,10 +24,7 @@ public class PaymentServiceImpl implements PaymentService {

@Override
public Page<PaymentEntity> search(SearchCriteriaDTO<PaymentCriteriaDTO> searchCriteria, Pageable pageable) {
// UUID enterpriseId = SecurityUtils.getCurrentUserEnterpriseId().orElseThrow();
return payRepo.findByName(
searchCriteria.criteria().criteria(),
// enterpriseId,
pageable);
UUID enterpriseId = SecurityUtils.getCurrentUserEnterpriseId().orElseThrow();
return payRepo.findByEnterpriseId(enterpriseId, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
@RequiredArgsConstructor
public class WalletServiceImpl implements WalletService {
private final WalletRepository walRepo;

@Override
public WalletEntity getBalance(){
// UUID enterpriseId = SecurityUtils.getCurrentUserEnterpriseId().orElseThrow();
//TODO are here

return new WalletEntity();
public Long getBalance(){
UUID enterpriseId = SecurityUtils.getCurrentUserEnterpriseId().orElseThrow();
return walRepo
.findByEnterpriseId(enterpriseId)
.map(WalletEntity::getBalance)
.orElseThrow();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</p>
</div>
<div
class="flex items-center justify-center lg:justify-between gap-2.5 lg:flex-row flex-col mb-20 lg:pl-20 pl-0"
class="flex items-center justify-center lg:justify-between gap-2.5 lg:flex-row flex-col mb-20 lg:px-10 pl-0"
>
<div class="flex gap-6 flex-col items-center lg:items-start">
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Component, OnInit, TemplateRef, ViewChild} from '@angular/core';
import {SubscriptionAwareComponent} from '../../../core/subscription-aware.component';
import {TableTemplateColumn} from '../../../shared/components/table-template/table-template.component';
import {
SearchCriteriaDto,
Expand All @@ -8,6 +9,7 @@ import {Observable} from 'rxjs';
import {ApplicationService} from '../../../core/services/application.service';
import {PaymentDTO} from '../../models/payment';
import {PaymentService} from '../../services/payment.service';
import {WalletService} from '../../services/wallet.service';

export interface PaymentCriteria {
criteria: string;
Expand All @@ -19,7 +21,10 @@ export interface PaymentCriteria {
templateUrl: './payment.component.html',
styleUrl: './payment.component.css'
})
export class PaymentComponent implements OnInit {
export class PaymentComponent
extends SubscriptionAwareComponent
implements OnInit
{
@ViewChild('statusTemplate', {static: true})
statusTemplate!: TemplateRef<any>;
@ViewChild('amountTemplate', {static: true})
Expand All @@ -32,10 +37,14 @@ export class PaymentComponent implements OnInit {
protected fetchData!: (
criteria: SearchCriteriaDto<PaymentCriteria>
) => Observable<SearchResultDto<PaymentDTO>>;

constructor(
protected readonly applicationService: ApplicationService,
private readonly paymentService: PaymentService
) {}
private readonly paymentService: PaymentService,
private readonly walletService: WalletService
) {
super();
}

ngOnInit(): void {
this.fetchData = this.paymentService.getPayments.bind(this.paymentService);
Expand Down Expand Up @@ -75,6 +84,10 @@ export class PaymentComponent implements OnInit {
}

getBalance(): void {
this.balance = 1500;
this.registerSubscription(
this.walletService.getWalletBalance().subscribe(result => {
this.balance = result;
})
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Component, OnInit} from '@angular/core';
import {SubscriptionAwareComponent} from '../../../core/subscription-aware.component';
import {WalletService} from '../../services/wallet.service';

interface CreditPackage {
id: number;
Expand All @@ -10,7 +12,10 @@ interface CreditPackage {
templateUrl: './plan.component.html',
styleUrl: './plan.component.css'
})
export class PlanComponent implements OnInit {
export class PlanComponent
extends SubscriptionAwareComponent
implements OnInit
{
tabs = [
{title: 'purchaseCredit.about', content: 'Content 1', value: '0'},
{title: 'purchaseCredit.termService', content: 'Content 2', value: '1'}
Expand All @@ -26,7 +31,9 @@ export class PlanComponent implements OnInit {

selectedPackageId: number | null = null;
balance: number = 0;
constructor() {}
constructor(private readonly walletService: WalletService) {
super();
}
ngOnInit(): void {
this.getBalance();
}
Expand All @@ -40,6 +47,10 @@ export class PlanComponent implements OnInit {
}
}
getBalance(): void {
this.balance = 1500;
this.registerSubscription(
this.walletService.getWalletBalance().subscribe(result => {
this.balance = result;
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {MarkerService} from './services/marker.service';
import {PaymentService} from './services/payment.service';
import {PopupService} from './services/popup.service';
import {RegionService} from './services/region.service';
import {WalletService} from './services/wallet.service';

@NgModule({
declarations: [
Expand All @@ -18,6 +19,12 @@ import {RegionService} from './services/region.service';
BuildingsComponent
],
imports: [SharedModule, EnterpriseRoutingModule],
providers: [MarkerService, PopupService, RegionService, PaymentService]
providers: [
MarkerService,
PopupService,
RegionService,
PaymentService,
WalletService
]
})
export class EnterpriseModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {AppRoutingConstants} from '../../../app-routing.constant';

@Injectable()
export class WalletService {
constructor(private readonly httpClient: HttpClient) {}

public getWalletBalance(): Observable<number> {
return this.httpClient.get<number>(
`${AppRoutingConstants.ENTERPRISE_API_URL}/wallet/balance`
);
}
}
4 changes: 2 additions & 2 deletions sep490-frontend/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@
"amount": "Amount"
},
"button": {
"buyCredit": "Buy Credit",
"buyCredit": "Buy Credits",
"setting": "Change Setting"
},
"credit": {
"label" : "Credit is the conversion unit that the system allocates to businesses.",
"title": "Credit"
"title": "Credits"
}
},
"status": {
Expand Down

0 comments on commit 922d111

Please sign in to comment.