Skip to content

feat: adding crud + csv for currency #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: release/1.1.0
Choose a base branch
from
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
9 changes: 8 additions & 1 deletion docs/csvImporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ It is possible to import the following data:
- Ref Codes
- Report Type Field mapping
- Cost Center
- Currency


## Data structure
Expand Down Expand Up @@ -93,4 +94,10 @@ The following columns are needed:
- `Customer code`: Code of the project (e.g. `PROJECT_1`)
- `External customer code`: Name of the external project code (e.g. `Project 1`)
- `Name`: Name of the project (e.g. `Project 1`)
- `Parent customer code`: Parent code of the project (e.g. `PROJECT_2`)
- `Parent customer code`: Parent code of the project (e.g. `PROJECT_2`)

### Currency data structure
An example can be found here: [Currency CSV Example](./examples/currency_csv_example.csv)
The following columns are needed:
- `Currency`: Currency code (e.g. `CHF`)
- `Name`: Name of the currency (e.g. `ISO_4217:CHF`)
2 changes: 2 additions & 0 deletions docs/examples/currency_csv_example.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Customer Code,Currency ID
CHF,ISO_4217:CHF
2 changes: 2 additions & 0 deletions docs/examples/vat_csv_example.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Customer Code,Rate,Parent Customer Code,Description,Active
VAT9,0.1,,Vat Code123,True
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

import org.springframework.data.domain.Persistable;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -17,6 +19,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "organisation_currency")
@Audited
Expand All @@ -28,6 +31,7 @@ public class OrganisationCurrency extends CommonEntity implements Persistable<Or
@AttributeOverride(name = "organisationId", column = @Column(name = "organisation_id")),
@AttributeOverride(name = "customerCode", column = @Column(name = "customer_code"))
})
@NonNull
private Id id;

@Column(name = "currency_id", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.cardanofoundation.lob.app.organisation.domain.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import com.opencsv.bean.CsvBindByName;
import io.swagger.v3.oas.annotations.media.Schema;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CurrencyUpdate {

@Schema(example = "CHF")
@CsvBindByName(column = "Customer Code", required = true)
private String customerCode;
@Schema(example = "ISO_4217:CHF")
@CsvBindByName(column = "Currency ID", required = true)
private String currencyId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.cardanofoundation.lob.app.organisation.domain.view;

import java.util.Optional;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.zalando.problem.Problem;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CurrencyView {

private String customerCode;
private String currencyId;

private Optional<Problem> error;

public static CurrencyView createFail(Problem error, String customerCode) {
return new CurrencyView(customerCode, null, Optional.of(error));
}

public static CurrencyView createSuccess(String customerCode, String currencyId) {
return new CurrencyView(customerCode, currencyId, Optional.empty());
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.cardanofoundation.lob.app.organisation.resource;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;

import java.util.Set;
import java.util.stream.Collectors;

import jakarta.validation.Valid;

import lombok.RequiredArgsConstructor;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.cardanofoundation.lob.app.organisation.domain.csv.CostCenterUpdate;
import org.cardanofoundation.lob.app.organisation.domain.view.OrganisationCostCenterView;
import org.cardanofoundation.lob.app.organisation.service.CostCenterService;

@RestController
@RequestMapping("/api")
@Tag(name = "Organisation", description = "Organisation API")
@CrossOrigin(origins = "http://localhost:3000")
@RequiredArgsConstructor
@ConditionalOnProperty(value = "lob.organisation.enabled", havingValue = "true", matchIfMissing = true)
public class CostCenterController {

private final CostCenterService costCenterService;

@Operation(description = "Organisation cost center", responses = {
@ApiResponse(content =
{@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = OrganisationCostCenterView.class)))}
),
})
@GetMapping(value = "/organisation/{orgId}/cost-center", produces = "application/json")
public ResponseEntity<Set<OrganisationCostCenterView>> getAllCostCenters(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId) {
return ResponseEntity.ok().body(
costCenterService.getAllCostCenter(orgId).stream().map(OrganisationCostCenterView::fromEntity).collect(Collectors.toSet()));
}

@Operation(description = "Organisation cost center creation", responses = {
@ApiResponse(content =
{@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = OrganisationCostCenterView.class)))}
),
})
@PostMapping(value = "/organisation/{orgId}/cost-center/insert", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole(@securityConfig.getManagerRole()) or hasRole(@securityConfig.getAdminRole())")
public ResponseEntity<OrganisationCostCenterView> insertCostCenters(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId, @Valid @RequestBody CostCenterUpdate costCenterUpdate) {
return ResponseEntity.ok(costCenterService.insertCostCenter(orgId, costCenterUpdate));
}

@Operation(description = "Organisation cost center update", responses = {
@ApiResponse(content =
{@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = OrganisationCostCenterView.class)))}
),
})
@PostMapping(value = "/organisation/{orgId}/cost-center/update", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole(@securityConfig.getManagerRole()) or hasRole(@securityConfig.getAdminRole())")
public ResponseEntity<OrganisationCostCenterView> updateCostCenters(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId, @Valid @RequestBody CostCenterUpdate costCenterUpdate) {
return ResponseEntity.ok(costCenterService.updateCostCenter(orgId, costCenterUpdate));
}

@Operation(description = "Organisation cost center creation csv", responses = {
@ApiResponse(content =
{@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = OrganisationCostCenterView.class)))}
),
})
@PostMapping(value = "/organisation/{orgId}/cost-center-csv", produces = APPLICATION_JSON_VALUE, consumes = MULTIPART_FORM_DATA_VALUE)
@PreAuthorize("hasRole(@securityConfig.getManagerRole()) or hasRole(@securityConfig.getAdminRole())")
public ResponseEntity<?> insertCostCentersCsv(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId, @RequestParam("file") MultipartFile file) {
return costCenterService.createCostCenterFromCsv(orgId, file).fold(
problem -> ResponseEntity.status(BAD_REQUEST).body(problem),
ResponseEntity::ok
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.cardanofoundation.lob.app.organisation.resource;

import java.util.List;
import java.util.Objects;

import jakarta.validation.Valid;

import lombok.RequiredArgsConstructor;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.cardanofoundation.lob.app.organisation.domain.request.CurrencyUpdate;
import org.cardanofoundation.lob.app.organisation.domain.view.CurrencyView;
import org.cardanofoundation.lob.app.organisation.service.CurrencyService;

@RestController
@RequestMapping("/api/organisation")
@Tag(name = "Organisation", description = "Organisation API")
@CrossOrigin(origins = "http://localhost:3000")
@RequiredArgsConstructor
@ConditionalOnProperty(value = "lob.organisation.enabled", havingValue = "true", matchIfMissing = true)
public class CurrencyController {

private final CurrencyService currencyService;

@Operation(description = "Get all currencies", responses = {
@ApiResponse(content =
{@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = CurrencyView.class)))}
),
})
@GetMapping(value = "/{orgId}/currencies", produces = "application/json")
public ResponseEntity<List<CurrencyView>> getAllCurrencies(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId) {
return ResponseEntity.ok().body(currencyService.getAllCurrencies(orgId));
}

@Operation(description = "Get currency", responses = {
@ApiResponse(content =
{@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = CurrencyView.class)))}
),
})
@GetMapping(value = "/{orgId}/currencies/{customerCode}", produces = "application/json")
public ResponseEntity<CurrencyView> getCurrency(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId, @PathVariable("customerCode") @Parameter(example = "CHF") String customerCode) {
return currencyService.getCurrency(orgId, customerCode)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}

@Operation(description = "Currency Insert", responses = {
@ApiResponse(content =
{@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = CurrencyView.class)))}
),
})
@PostMapping(value = "/{orgId}/currencies/insert", produces = "application/json")
@PreAuthorize("hasRole(@securityConfig.getManagerRole()) or hasRole(@securityConfig.getAccountantRole()) or hasRole(@securityConfig.getAdminRole())")
public ResponseEntity<CurrencyView> insertCurrency(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId, @Valid @RequestBody CurrencyUpdate currencyUpdate) {
return ResponseEntity.ok().body(currencyService.insertCurrency(orgId, currencyUpdate));
}

@Operation(description = "Currency Update", responses = {
@ApiResponse(content =
{@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = CurrencyView.class)))}
),
})
@PostMapping(value = "/{orgId}/currencies/update", produces = "application/json")
@PreAuthorize("hasRole(@securityConfig.getManagerRole()) or hasRole(@securityConfig.getAccountantRole()) or hasRole(@securityConfig.getAdminRole())")
public ResponseEntity<CurrencyView> updateCurrency(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId, @Valid @RequestBody CurrencyUpdate currencyUpdate) {
return ResponseEntity.ok().body(currencyService.updateCurrency(orgId, currencyUpdate));
}

@Operation(description = "Currency Upload", responses = {
@ApiResponse(content =
{@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = CurrencyView.class)))}
),
})
@PostMapping(value = "/{orgId}/currencies/upload", produces = "application/json")
@PreAuthorize("hasRole(@securityConfig.getManagerRole()) or hasRole(@securityConfig.getAccountantRole()) or hasRole(@securityConfig.getAdminRole())")
public ResponseEntity<?> insertCurrenciesCsv(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId, @RequestParam(value = "file") MultipartFile file) {
return currencyService.insertViaCsv(orgId, file).fold(
problem -> ResponseEntity.status(Objects.requireNonNull(problem.getStatus()).getStatusCode()).body(problem),
ResponseEntity::ok
);
}
}
Loading
Loading