-
Notifications
You must be signed in to change notification settings - Fork 0
Add exchange rate #114
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
Merged
Merged
Add exchange rate #114
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/dev/vality/daway/dao/invoicing/iface/PaymentExchangeContextDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package dev.vality.daway.dao.invoicing.iface; | ||
|
|
||
| import dev.vality.dao.GenericDao; | ||
| import dev.vality.daway.domain.tables.pojos.PaymentExchangeContext; | ||
| import dev.vality.daway.exception.DaoException; | ||
| import dev.vality.daway.model.InvoicingKey; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public interface PaymentExchangeContextDao extends GenericDao { | ||
|
|
||
| void saveBatch(List<PaymentExchangeContext> paymentExchangeContexts) throws DaoException; | ||
|
|
||
| void switchCurrent(Set<InvoicingKey> invoicingKeys) throws DaoException; | ||
|
|
||
| } |
82 changes: 82 additions & 0 deletions
82
src/main/java/dev/vality/daway/dao/invoicing/impl/PaymentExchangeContextDaoImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package dev.vality.daway.dao.invoicing.impl; | ||
|
|
||
| import dev.vality.dao.impl.AbstractGenericDao; | ||
| import dev.vality.daway.dao.invoicing.iface.PaymentExchangeContextDao; | ||
| import dev.vality.daway.domain.tables.pojos.PaymentExchangeContext; | ||
| import dev.vality.daway.domain.tables.records.PaymentExchangeContextRecord; | ||
| import dev.vality.daway.exception.DaoException; | ||
| import dev.vality.daway.model.InvoicingKey; | ||
| import dev.vality.mapper.RecordRowMapper; | ||
| import org.jooq.Query; | ||
| import org.jooq.impl.DSL; | ||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static dev.vality.daway.domain.Tables.PAYMENT_EXCHANGE_CONTEXT; | ||
|
|
||
| @Component | ||
| public class PaymentExchangeContextDaoImpl extends AbstractGenericDao implements PaymentExchangeContextDao { | ||
|
|
||
| private final RowMapper<PaymentExchangeContext> paymentExchangeContextRowMapper; | ||
|
|
||
| public PaymentExchangeContextDaoImpl(DataSource dataSource) { | ||
| super(dataSource); | ||
| paymentExchangeContextRowMapper = new RecordRowMapper<>(PAYMENT_EXCHANGE_CONTEXT, PaymentExchangeContext.class); | ||
| } | ||
|
|
||
| @Override | ||
| public void saveBatch(List<PaymentExchangeContext> paymentExchangeContexts) throws DaoException { | ||
| List<Query> queries = paymentExchangeContexts.stream() | ||
| .map(paymentExchangeContext -> getDslContext().newRecord( | ||
| PAYMENT_EXCHANGE_CONTEXT, paymentExchangeContext)) | ||
| .map(this::prepareInsertQuery) | ||
| .toList(); | ||
| batchExecute(queries); | ||
| } | ||
|
|
||
| private Query prepareInsertQuery(PaymentExchangeContextRecord record) { | ||
| return getDslContext().insertInto(PAYMENT_EXCHANGE_CONTEXT) | ||
| .set(record) | ||
| .onConflict( | ||
| PAYMENT_EXCHANGE_CONTEXT.INVOICE_ID, | ||
| PAYMENT_EXCHANGE_CONTEXT.PAYMENT_ID, | ||
| PAYMENT_EXCHANGE_CONTEXT.SEQUENCE_ID, | ||
| PAYMENT_EXCHANGE_CONTEXT.CHANGE_ID | ||
| ) | ||
| .doNothing(); | ||
| } | ||
|
|
||
| @Override | ||
| public void switchCurrent(Set<InvoicingKey> invoicingKeys) throws DaoException { | ||
| invoicingKeys.forEach(key -> { | ||
| setOldPaymentExchangeContextNotCurrent(key); | ||
| setLatestPaymentExchangeContextCurrent(key); | ||
| }); | ||
| } | ||
|
|
||
| private void setOldPaymentExchangeContextNotCurrent(InvoicingKey key) { | ||
| execute(getDslContext().update(PAYMENT_EXCHANGE_CONTEXT) | ||
| .set(PAYMENT_EXCHANGE_CONTEXT.CURRENT, false) | ||
| .where(PAYMENT_EXCHANGE_CONTEXT.INVOICE_ID.eq(key.getInvoiceId()) | ||
| .and(PAYMENT_EXCHANGE_CONTEXT.PAYMENT_ID.eq(key.getPaymentId())) | ||
| .and(PAYMENT_EXCHANGE_CONTEXT.CURRENT)) | ||
| ); | ||
| } | ||
|
|
||
| private void setLatestPaymentExchangeContextCurrent(InvoicingKey key) { | ||
| execute(getDslContext().update(PAYMENT_EXCHANGE_CONTEXT) | ||
| .set(PAYMENT_EXCHANGE_CONTEXT.CURRENT, true) | ||
| .where(PAYMENT_EXCHANGE_CONTEXT.ID.eq( | ||
| DSL.select(DSL.max(PAYMENT_EXCHANGE_CONTEXT.ID)) | ||
| .from(PAYMENT_EXCHANGE_CONTEXT) | ||
| .where(PAYMENT_EXCHANGE_CONTEXT.INVOICE_ID.eq(key.getInvoiceId()) | ||
| .and(PAYMENT_EXCHANGE_CONTEXT.PAYMENT_ID.eq(key.getPaymentId()))) | ||
| )) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...n/java/dev/vality/daway/handler/wrapper/payment/PaymentExchangeContextWrapperHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package dev.vality.daway.handler.wrapper.payment; | ||
|
|
||
| import dev.vality.daway.dao.invoicing.iface.PaymentExchangeContextDao; | ||
| import dev.vality.daway.domain.tables.pojos.PaymentExchangeContext; | ||
| import dev.vality.daway.handler.wrapper.WrapperHandler; | ||
| import dev.vality.daway.model.PaymentWrapper; | ||
| import dev.vality.daway.util.PaymentWrapperUtil; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class PaymentExchangeContextWrapperHandler implements WrapperHandler<PaymentWrapper> { | ||
|
|
||
| private final PaymentExchangeContextDao paymentExchangeContextDao; | ||
|
|
||
| @Override | ||
| public boolean accept(List<PaymentWrapper> wrappers) { | ||
| return wrappers.stream() | ||
| .map(PaymentWrapper::getPaymentExchangeContext) | ||
| .anyMatch(Objects::nonNull); | ||
| } | ||
|
|
||
| @Override | ||
| public void saveBatch(List<PaymentWrapper> wrappers) { | ||
| List<PaymentWrapper> processableWrappers = wrappers.stream() | ||
| .filter(paymentWrapper -> Objects.nonNull(paymentWrapper.getPaymentExchangeContext())) | ||
| .collect(Collectors.toList()); | ||
| List<PaymentExchangeContext> paymentExchangeContexts = processableWrappers.stream() | ||
| .map(PaymentWrapper::getPaymentExchangeContext) | ||
| .collect(Collectors.toList()); | ||
| paymentExchangeContextDao.saveBatch(paymentExchangeContexts); | ||
| paymentExchangeContextDao.switchCurrent(PaymentWrapperUtil.getInvoicingKeys(processableWrappers)); | ||
| } | ||
| } | ||
73 changes: 73 additions & 0 deletions
73
...main/java/dev/vality/daway/mapper/payment/InvoicePaymentExchangeContextChangedMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package dev.vality.daway.mapper.payment; | ||
|
|
||
| import dev.vality.damsel.base.Rational; | ||
| import dev.vality.damsel.domain.ExchangeContext; | ||
| import dev.vality.damsel.payment_processing.InvoiceChange; | ||
| import dev.vality.damsel.payment_processing.InvoicePaymentChange; | ||
| import dev.vality.damsel.payment_processing.InvoicePaymentExchangeContextChanged; | ||
| import dev.vality.daway.domain.tables.pojos.PaymentExchangeContext; | ||
| import dev.vality.daway.mapper.Mapper; | ||
| import dev.vality.daway.model.InvoicingKey; | ||
| import dev.vality.daway.model.PaymentWrapper; | ||
| import dev.vality.geck.common.util.TypeUtil; | ||
| import dev.vality.geck.filter.Filter; | ||
| import dev.vality.geck.filter.PathConditionFilter; | ||
| import dev.vality.geck.filter.condition.IsNullCondition; | ||
| import dev.vality.geck.filter.rule.PathConditionRule; | ||
| import dev.vality.machinegun.eventsink.MachineEvent; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class InvoicePaymentExchangeContextChangedMapper implements Mapper<PaymentWrapper> { | ||
|
|
||
| private Filter filter = new PathConditionFilter(new PathConditionRule( | ||
| "invoice_payment_change.payload.invoice_payment_exchange_context_changed", | ||
| new IsNullCondition().not())); | ||
|
|
||
| @Override | ||
| public PaymentWrapper map(InvoiceChange change, MachineEvent event, Integer changeId) { | ||
| InvoicePaymentChange invoicePaymentChange = change.getInvoicePaymentChange(); | ||
| String invoiceId = event.getSourceId(); | ||
| String paymentId = invoicePaymentChange.getId(); | ||
| long sequenceId = event.getEventId(); | ||
| log.info("Start mapping payment exchange context change, sequenceId='{}', changeId='{}', invoiceId='{}', " | ||
| + "paymentId='{}'", | ||
| sequenceId, changeId, invoiceId, paymentId); | ||
|
|
||
| InvoicePaymentExchangeContextChanged exchangeContextChanged = | ||
| invoicePaymentChange.getPayload().getInvoicePaymentExchangeContextChanged(); | ||
| ExchangeContext exchangeContext = exchangeContextChanged.getExchangeContext(); | ||
| Rational exchangeRate = exchangeContext.getExchangeRate(); | ||
|
|
||
| PaymentExchangeContext paymentExchangeContext = new PaymentExchangeContext(); | ||
| paymentExchangeContext.setWtime(null); | ||
| paymentExchangeContext.setId(null); | ||
| paymentExchangeContext.setChangeId(changeId); | ||
| paymentExchangeContext.setSequenceId(sequenceId); | ||
| paymentExchangeContext.setPaymentId(paymentId); | ||
| paymentExchangeContext.setInvoiceId(invoiceId); | ||
| paymentExchangeContext.setCurrent(true); | ||
| paymentExchangeContext.setEventCreatedAt(TypeUtil.stringToLocalDateTime(event.getCreatedAt())); | ||
| paymentExchangeContext.setSourceCurrencyCode(exchangeContext.getSourceCurrency()); | ||
| paymentExchangeContext.setDestinationCurrencyCode(exchangeContext.getDestinationCurrency()); | ||
| paymentExchangeContext.setExchangeRateRationalP(exchangeRate.getP()); | ||
| paymentExchangeContext.setExchangeRateRationalQ(exchangeRate.getQ()); | ||
|
|
||
| PaymentWrapper paymentWrapper = new PaymentWrapper(); | ||
| paymentWrapper.setKey(InvoicingKey.buildKey(invoiceId, paymentId)); | ||
| paymentWrapper.setPaymentExchangeContext(paymentExchangeContext); | ||
| log.info("Payment exchange context has been mapped, sequenceId='{}', changeId='{}', invoiceId='{}', " | ||
| + "paymentId='{}'", | ||
| sequenceId, changeId, invoiceId, paymentId); | ||
| return paymentWrapper; | ||
| } | ||
|
|
||
| @Override | ||
| public Filter<InvoiceChange> getFilter() { | ||
| return filter; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/resources/db/migration/V52__add_payment_exchange_context.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| CREATE TABLE IF NOT EXISTS dw.payment_exchange_context | ||
| ( | ||
| id bigserial NOT NULL, | ||
| event_created_at timestamp without time zone NOT NULL, | ||
| invoice_id character varying NOT NULL, | ||
| payment_id character varying NOT NULL, | ||
| source_currency_code character varying NOT NULL, | ||
| destination_currency_code character varying NOT NULL, | ||
| exchange_rate_rational_p bigint NOT NULL, | ||
| exchange_rate_rational_q bigint NOT NULL, | ||
| current boolean NOT NULL DEFAULT false, | ||
| wtime timestamp without time zone NOT NULL DEFAULT (now() AT TIME ZONE 'utc'::text), | ||
| sequence_id bigint, | ||
| change_id integer, | ||
|
|
||
| CONSTRAINT payment_exchange_context_pkey PRIMARY KEY (id), | ||
| CONSTRAINT payment_exchange_context_uniq UNIQUE (invoice_id, payment_id, sequence_id, change_id) | ||
| ); | ||
|
|
||
| ALTER TABLE dw.cash_flow | ||
| ADD COLUMN IF NOT EXISTS exchange_source_currency_code character varying, | ||
| ADD COLUMN IF NOT EXISTS exchange_destination_currency_code character varying, | ||
| ADD COLUMN IF NOT EXISTS exchange_rate_rational_p bigint, | ||
| ADD COLUMN IF NOT EXISTS exchange_rate_rational_q bigint; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
кажется, что можно без промежуточной переменной, в одном stream все это сделать, просто продолжить stream выше
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тоже так думал но когда поправил увидел что ниже они обе используются