Skip to content
Open
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
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id("java")
id("com.diffplug.spotless") version "8.1.0"
id("org.openrewrite.rewrite") version "7.23.0"
id("org.flywaydb.flyway") version "10.0.0"
}

group = "com.razdeep"
Expand Down Expand Up @@ -61,6 +62,8 @@ dependencies {
implementation(libs.spring.boot.starter.cache)
implementation(libs.jackson.jsr310)

implementation("org.flywaydb:flyway-core")

runtimeOnly(libs.micrometer.prometheus)

testImplementation(libs.spring.boot.starter.test)
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ services:
image: postgres:15
container_name: postgres
restart: always
command:
- "postgres"
- "-c"
- "shared_preload_libraries=pg_stat_statements"
environment:
POSTGRES_USER: konsign
POSTGRES_PASSWORD: konsign
Expand All @@ -13,6 +17,7 @@ services:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./postgres/init:/docker-entrypoint-initdb.d

redis:
image: bitnami/redis:latest
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[versions]
springboot = "3.0.6"
springboot = "3.5.10"
jjwt = "0.9.1"
jaxb = "2.4.0-b180830.0359"
postgres = "42.7.8"
lombok = "1.18.30" # put actual version you use
lombokmapstruct = "0.2.0"
mapstruct = "1.6.3"
springdoc = "1.6.14"
springdoc = "1.8.0"
jedis = "4.4.2"
jackson = "2.15.0"
micrometer = "1.11.0" # adjust if needed
Expand Down
19 changes: 19 additions & 0 deletions postgres/init/01-create-app-user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- create application role
CREATE ROLE app_user
LOGIN
PASSWORD 'app_password'
NOSUPERUSER
NOCREATEDB
NOCREATEROLE
NOINHERIT
NOBYPASSRLS;

-- optional: readonly / app schema access
GRANT CONNECT ON DATABASE konsign TO app_user;

GRANT USAGE ON SCHEMA public TO app_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user;

-- future tables
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_user;
7 changes: 4 additions & 3 deletions src/main/java/com/razdeep/konsignapi/entity/KonsignUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
@Setter
public class KonsignUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "app_user_seq_gen")
@SequenceGenerator(name = "app_user_seq_gen", sequenceName = "app_user_seq", allocationSize = 1)
private long id;

@Column(name = "username", unique = true)
private String username;

private String password;
private String emailAddress;
private String email;
private String mobile;
private String tenantId;
private boolean active;
Expand All @@ -29,7 +30,7 @@ public KonsignUser() {}
public KonsignUser(UserRegistration userRegistration) {
username = userRegistration.getUsername();
password = userRegistration.getPassword();
emailAddress = userRegistration.getEmailAddress();
email = userRegistration.getEmail();
mobile = userRegistration.getMobile();
tenantId = userRegistration.getTenantId();
active = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class UserRegistration {
@NonNull
private String username;

private String emailAddress;
private String email;

private String mobile;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.razdeep.konsignapi.entity.BillEntity;
import java.util.List;
import java.util.Optional;
import lombok.NonNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -15,7 +16,8 @@ public interface BillEntryRepository extends JpaRepository<BillEntity, String> {
@Query(value = "select billEntity from BillEntity billEntity where billEntity.buyerEntity.buyerId = ?1")
List<BillEntity> findAllBillsByBuyerId(String buyerId);

Optional<BillEntity> findByBillNoAndTenantId(String billNo, String tenantId);
Optional<BillEntity> findByBillNo(String billNo);

Page<BillEntity> findByTenantId(String tenantId, Pageable pageable);
@NonNull
Page<BillEntity> findAll(@NonNull Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@

@Repository
public interface BuyerRepository extends JpaRepository<BuyerEntity, String> {
List<BuyerEntity> findAllBuyerByBuyerNameAndTenantId(@NonNull String buyerName, @NonNull String tenantId);
List<BuyerEntity> findAllBuyerByBuyerName(@NonNull String buyerName);

List<BuyerEntity> findAllByTenantId(@NonNull String tenantId);
@NonNull
List<BuyerEntity> findAll();

Optional<BuyerEntity> findByBuyerIdAndTenantId(@NonNull String buyerId, @NonNull String tenantId);
Optional<BuyerEntity> findByBuyerId(@NonNull String buyerId);

@Query("""
select b.buyerName
from BuyerEntity b
where b.buyerId = :buyerId
and b.tenantId = :tenantId
""")
String findBuyerNameByBuyerId(String buyerId, String tenantId);
String findBuyerNameByBuyerId(String buyerId);

@Query(value = """
with collection_joined as (
Expand All @@ -33,8 +33,7 @@ with collection_joined as (
collection_voucher
join collection_voucher_item on
voucher_no = fk_collection_voucher_id
where buyer_buyer_id = :buyerId
and collection_voucher.tenant_id = :tenantId),
where buyer_buyer_id = :buyerId),
bill_joined as (
select
*
Expand All @@ -61,5 +60,5 @@ bill_joined as (
on
bill_joined.bill_no = collection_joined.bill_bill_no;
""", nativeQuery = true)
List<BillCollectionProjection> computeBuyerLedger(@NonNull String buyerId, @NonNull String tenantId);
List<BillCollectionProjection> computeBuyerLedger(@NonNull String buyerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import com.razdeep.konsignapi.entity.SupplierEntity;
import java.util.List;
import java.util.Optional;
import lombok.NonNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface SupplierRepository extends JpaRepository<SupplierEntity, String> {

List<SupplierEntity> findAllSupplierBySupplierNameAndTenantId(String supplierName, String tenantId);
List<SupplierEntity> findAllSupplierBySupplierName(String supplierName);

List<SupplierEntity> findAllByTenantId(String supplierName);
@NonNull
List<SupplierEntity> findAll();

Optional<SupplierEntity> findSupplierBySupplierIdAndTenantId(String supplierId, String tenantId);
Optional<SupplierEntity> findSupplierBySupplierId(String supplierId);

@Query("""
select s.supplierName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import com.razdeep.konsignapi.entity.TransportEntity;
import java.util.List;
import java.util.Optional;
import lombok.NonNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TransportRepository extends JpaRepository<TransportEntity, String> {

List<TransportEntity> findAllTransportByTransportNameAndTenantId(String transportName, String tenantId);
List<TransportEntity> findAllTransportByTransportName(String transportName);

List<TransportEntity> findAllByTenantId(String tenantId);
@NonNull
List<TransportEntity> findAll();

Optional<TransportEntity> findByTransportIdAndTenantId(String transportId, String tenantId);
Optional<TransportEntity> findByTransportId(String transportId);

void deleteByTransportIdAndTenantId(String transportId, String tenantId);
void deleteByTransportId(String transportId);
}
11 changes: 3 additions & 8 deletions src/main/java/com/razdeep/konsignapi/service/BillService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,9 @@ public void addBill(Bill bill) {
billEntryRepository.save(billEntity);
}

public Bill getBill(String billNo) {
String tenantId = commonService.getTenantId();
return getBill(billNo, tenantId);
}

public Bill getBill(String billNo, String tenantId) throws ResourceNotFoundException {
public Bill getBill(String billNo) throws ResourceNotFoundException {

final var billEntryOptional = billEntryRepository.findByBillNoAndTenantId(billNo, tenantId);
final var billEntryOptional = billEntryRepository.findByBillNo(billNo);
if (billEntryOptional.isEmpty()) {
throw new ResourceNotFoundException("Bill " + billNo + " not found");
}
Expand Down Expand Up @@ -117,7 +112,7 @@ public CustomPageImpl<Bill> getAllBills(int offset, int size, String agencyId) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Pageable pageable = PageRequest.of(offset, size, Sort.by("billNo").descending());
final var billEntityPages = billEntryRepository.findByTenantId(agencyId, pageable);
final var billEntityPages = billEntryRepository.findAll(pageable);
stopWatch.stop();
LOG.info("repository call took {} ms", stopWatch.getLastTaskTimeMillis());

Expand Down
24 changes: 6 additions & 18 deletions src/main/java/com/razdeep/konsignapi/service/BuyerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ public BuyerService(BuyerRepository buyerRepository, CommonService commonService
}

public List<Buyer> getBuyers() {
String agencyId = commonService.getTenantId();
return getBuyersByAgencyId(agencyId);
}

// @Cacheable(value = "getBuyers", key = "#agencyId")
public List<Buyer> getBuyersByAgencyId(String agencyId) {
List<Buyer> result = new ArrayList<>();
buyerRepository.findAllByTenantId(agencyId).forEach((buyerEntity) -> result.add(new Buyer(buyerEntity)));
buyerRepository.findAll().forEach((buyerEntity) -> result.add(new Buyer(buyerEntity)));
return result;
}

Expand All @@ -42,9 +36,7 @@ private boolean isBuyerIdTaken(String buyerId) {
// @CacheEvict(value = "getBuyers", allEntries = true)
public boolean addBuyer(Buyer buyer) {
String agencyId = commonService.getTenantId();
if (!buyerRepository
.findAllBuyerByBuyerNameAndTenantId(buyer.getBuyerName(), agencyId)
.isEmpty()) {
if (!buyerRepository.findAllBuyerByBuyerName(buyer.getBuyerName()).isEmpty()) {
return false;
}

Expand All @@ -69,29 +61,25 @@ public boolean addBuyer(Buyer buyer) {

// @CacheEvict(value = "getBuyers", allEntries = true)
public boolean deleteBuyer(String buyerId) {
String agencyId = commonService.getTenantId();
boolean wasPresent =
buyerRepository.findByBuyerIdAndTenantId(buyerId, agencyId).isPresent();
boolean wasPresent = buyerRepository.findByBuyerId(buyerId).isPresent();
if (wasPresent) {
buyerRepository.deleteById(buyerId);
}
return wasPresent;
}

public BuyerEntity getBuyerByBuyerName(String buyerName) {
String agencyId = commonService.getTenantId();
final var resultList = buyerRepository.findAllBuyerByBuyerNameAndTenantId(buyerName, agencyId);
final var resultList = buyerRepository.findAllBuyerByBuyerName(buyerName);
return resultList == null || resultList.isEmpty() ? null : resultList.get(0);
}

public byte[] generateBuyerLedger(String buyerId) throws Exception {
String agencyId = commonService.getTenantId();
Map<String, Object> payload = new HashMap<>();

String buyerName = buyerRepository.findBuyerNameByBuyerId(buyerId, agencyId);
String buyerName = buyerRepository.findBuyerNameByBuyerId(buyerId);
payload.put("buyerName", buyerName);

List<BillCollectionProjection> rows = buyerRepository.computeBuyerLedger(buyerId, agencyId);
List<BillCollectionProjection> rows = buyerRepository.computeBuyerLedger(buyerId);
List<BillCollectionDTO> items = rows.stream()
.map(rawRow -> new BillCollectionDTO(
rawRow.getBillNo(),
Expand Down
25 changes: 5 additions & 20 deletions src/main/java/com/razdeep/konsignapi/service/SupplierService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,16 @@ public boolean isSupplierIdTaken(String supplierId) {
}

public List<Supplier> getSuppliers() {
String agencyId = commonService.getTenantId();
return getSupplierByAgencyId(agencyId);
}

// @Cacheable(value = "getSuppliers", key = "#agencyId")
public List<Supplier> getSupplierByAgencyId(String agencyId) {
List<Supplier> result = new ArrayList<>();
List<SupplierEntity> supplierEntityList = supplierRepository.findAllByTenantId(agencyId);
if (supplierEntityList == null) {
return result;
}
List<SupplierEntity> supplierEntityList = supplierRepository.findAll();
supplierEntityList.forEach((supplierEntity) -> result.add(new Supplier(supplierEntity)));
return result;
}

// @CacheEvict(value = "getSuppliers", allEntries = true)
public boolean addSupplier(Supplier supplier) {
String agencyId = commonService.getTenantId();
if (!supplierRepository
.findAllSupplierBySupplierNameAndTenantId(supplier.getSupplierName(), agencyId)
.findAllSupplierBySupplierName(supplier.getSupplierName())
.isEmpty()) {
return false;
}
Expand All @@ -67,25 +57,20 @@ public boolean addSupplier(Supplier supplier) {
return true;
}

// @CacheEvict(value = "getSuppliers", allEntries = true)
public boolean deleteSupplier(String supplierId) {
String agencyId = commonService.getTenantId();
boolean wasPresent = supplierRepository
.findSupplierBySupplierIdAndTenantId(supplierId, agencyId)
.isPresent();
boolean wasPresent =
supplierRepository.findSupplierBySupplierId(supplierId).isPresent();
if (wasPresent) {
supplierRepository.deleteById(supplierId);
}
return wasPresent;
}

public SupplierEntity getSupplierBySupplierName(String supplierName) {
String agencyId = commonService.getTenantId();
final var resultList = supplierRepository.findAllSupplierBySupplierNameAndTenantId(supplierName, agencyId);
final var resultList = supplierRepository.findAllSupplierBySupplierName(supplierName);
return resultList == null || resultList.isEmpty() ? null : resultList.get(0);
}

// TODO: implement properly later
public byte[] generateSupplierLedger(String supplierId) throws Exception {
String supplierName = supplierRepository.findSupplierNameBySupplierId(supplierId);
Map<String, Object> payload = new HashMap<>();
Expand Down
Loading