Skip to content
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

feat: Export reward list of a period - MEED-7487 - Meeds-io/MIPs#154 #612

Merged
merged 2 commits into from
Nov 14, 2024
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
12 changes: 12 additions & 0 deletions wallet-api/src/main/java/io/meeds/wallet/utils/WalletUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,18 @@ public static final long getNetworkId() {
return settings == null || settings.getNetwork() == null ? 0 : settings.getNetwork().getId();
}

public static String getTransactionEtherScanLink() {
return switch (Math.toIntExact(getNetworkId())) {
case 1 -> "https://etherscan.io/tx/";
case 3 -> "https://ropsten.etherscan.io/tx/";
case 5 -> "https://goerli.etherscan.io/tx/";
case 137 -> "https://polygonscan.com/tx/";
case 80001 -> "https://mumbai.polygonscan.com/tx/";
case 80002 -> "https://amoy.polygonscan.com/tx/";
default -> "#";
};
}

public static final String getWebsocketURL() {
GlobalSettings settings = getSettings();
return settings == null || settings.getNetwork() == null ? null : settings.getNetwork().getWebsocketProviderURL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public PagedModel<EntityModel<WalletReward>> getWalletRewards(Pageable pageable,
return assembler.toModel(walletRewards);
}

@GetMapping(path = "/export")
@GetMapping(path = "export")
@Secured("rewarding")
@Operation(
summary = "Exports wallet rewards for a specified period as an XLSX file",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@
import static io.meeds.wallet.utils.RewardUtils.TRANSACTION_STATUS_PENDING;
import static io.meeds.wallet.utils.RewardUtils.TRANSACTION_STATUS_SUCCESS;
import static io.meeds.wallet.utils.RewardUtils.formatTime;
import static io.meeds.wallet.utils.WalletUtils.convertFromDecimals;
import static io.meeds.wallet.utils.WalletUtils.formatNumber;
import static io.meeds.wallet.utils.WalletUtils.getContractDetail;
import static io.meeds.wallet.utils.WalletUtils.getIdentityByTypeAndId;
import static io.meeds.wallet.utils.WalletUtils.getLocale;
import static io.meeds.wallet.utils.WalletUtils.getResourceBundleKey;
import static io.meeds.wallet.utils.WalletUtils.isUserRewardingAdmin;
import static io.meeds.wallet.utils.WalletUtils.*;

import java.io.*;
import java.math.BigInteger;
Expand All @@ -53,9 +47,8 @@
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.exoplatform.services.resources.ResourceBundleService;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -420,10 +413,7 @@ public Page<WalletReward> findWalletRewardsByPeriodIdAndStatus(long periodId, St
}

@Override
public InputStream exportXlsx(long periodId,
String status,
ZoneId zoneId,
String fileName, Locale locale) {
public InputStream exportXlsx(long periodId, String status, ZoneId zoneId, String fileName, Locale locale) {
File temp = null;
try { // NOSONAR
temp = createTempFile(fileName);
Expand Down Expand Up @@ -488,7 +478,9 @@ private void appendWalletRewardRow(Sheet sheet, int rowIndex, CreationHelper hel
row.createCell(cellIndex++).setCellValue(Utils.getUserFullName(String.valueOf(walletReward.getIdentityId())));
row.createCell(cellIndex++).setCellValue(walletReward.getWallet().getAddress());
row.createCell(cellIndex++).setCellValue(walletReward.getPoints());
row.createCell(cellIndex++).setCellValue(walletReward.getAmount());
double amount = walletReward.getAmount();
String amountText = (amount % 1 == 0) ? String.format("MEED %.0f", amount) : String.format("MEED %.2f", amount);
row.createCell(cellIndex++).setCellValue(amountText);
row.createCell(cellIndex++).setCellValue(walletReward.getStatus());
appendTransactionCells(row, cellIndex, helper, walletReward.getTransaction());
} catch (Exception e) {
Expand All @@ -499,7 +491,12 @@ private void appendWalletRewardRow(Sheet sheet, int rowIndex, CreationHelper hel
private void appendTransactionCells(Row row, int cellIndex, CreationHelper helper, TransactionDetail transaction) {
if (transaction != null) {
row.createCell(cellIndex++).setCellValue(helper.createRichTextString(String.valueOf(new Date(transaction.getSentTimestamp()))));
row.createCell(cellIndex).setCellValue(transaction.getHash());
Cell hashCell = row.createCell(cellIndex);
String hashUrl = getTransactionEtherScanLink() + transaction.getHash();
hashCell.setCellValue(transaction.getHash());
Hyperlink link = helper.createHyperlink(HyperlinkType.URL);
link.setAddress(hashUrl);
hashCell.setHyperlink(link);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.InputStream;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import java.util.Locale;

import io.meeds.wallet.model.*;
import io.meeds.wallet.utils.WalletUtils;
Expand Down Expand Up @@ -188,6 +190,38 @@ void getWalletRewardsAdmin() throws Exception {
response.andExpect(status().isOk());
}

@Test
void exportXlsxAnonymously() throws Exception {
ResultActions response = mockMvc.perform(get(REST_PATH + "/export").param("periodId", "1")
.param("status", "VALID")
.param("fileName", "fileName"));
response.andExpect(status().isForbidden());
}

@Test
void exportXlsxSimpleUser() throws Exception {
ResultActions response = mockMvc.perform(get(REST_PATH + "/export").param("periodId", "1")
.param("status", "VALID")
.param("fileName", "fileName")
.with(testSimpleUser()));
response.andExpect(status().isForbidden());
}

@Test
void exportXlsxAdmin() throws Exception {
RewardSettings rewardSettings = new RewardSettings();
when(rewardSettingsService.getSettings()).thenReturn(rewardSettings);
InputStream inputStream = mock(InputStream.class);

when(rewardReportService.exportXlsx(1, "VALID", rewardSettings.zoneId(), "fileName", Locale.ENGLISH)).thenReturn(inputStream);

ResultActions response = mockMvc.perform(get(REST_PATH + "/export").param("periodId", "1")
.param("status", "VALID")
.param("fileName", "fileName")
.with(testAdminUser()));
response.andExpect(status().isOk());
}

@Test
void computeRewardsByPeriodAnonymously() throws Exception {
ResultActions response = mockMvc.perform(post(REST_PATH + "/period/compute").content(asJsonString(rewardPeriod()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.meeds.gamification.model.filter.RealizationFilter;
import io.meeds.gamification.service.RealizationService;

import io.meeds.gamification.utils.Utils;
import io.meeds.wallet.model.*;
import io.meeds.wallet.utils.WalletUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -423,9 +422,8 @@ void testExportRewards() throws Exception {
assertNotNull(row1);
assertEquals(7, row1.getLastCellNum());
assertEquals(200, row1.getCell(2).getNumericCellValue());
assertEquals(50, row1.getCell(3).getNumericCellValue());
assertEquals("MEED 50", row1.getCell(3).getStringCellValue());
assertEquals("success", row1.getCell(4).getStringCellValue());
assertEquals("transactionHash", row1.getCell(6).getStringCellValue());
}

protected Wallet newWallet(long identityId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@
<template v-if="!$vuetify.breakpoint.mobile" #left>
<v-btn
:href="exportFileLink"
download="rewards-detail.xlsx"
class="ma-2"
:download="fileName"
color="primary"
outlined>
<v-icon
Expand Down Expand Up @@ -188,6 +187,11 @@ export default {
month: 'short',
day: 'numeric',
},
dateNumericFormat: {
year: 'numeric',
month: 'numeric',
day: 'numeric',
},
walletRewards: [],
selectedWalletReward: null,
status: 'VALID',
Expand Down Expand Up @@ -254,6 +258,19 @@ export default {
return new window.Intl.DateTimeFormat(this.lang, this.dateFormat)
.format(new Date(this.endDateInSeconds * 1000 - 86400 * 1000 - new Date().getTimezoneOffset() * 60 * 1000));
},
starDateNumericFormat() {
const formattedDate = new window.Intl.DateTimeFormat(this.lang, this.dateNumericFormat).format(new Date(this.startDateInSeconds * 1000 - new Date().getTimezoneOffset() * 60 * 1000));
return formattedDate.replace(/\//g, '-');
},
toDateNumericFormat() {
const formattedDate = new window.Intl.DateTimeFormat(this.lang, this.dateNumericFormat)
.format(new Date(this.endDateInSeconds * 1000 - 86400 * 1000 - new Date().getTimezoneOffset() * 60 * 1000));
return formattedDate.replace(/\//g, '-');
},
fileName() {
console.warn(this.starDateNumericFormat);
return `rewards-detail-from ${this.starDateNumericFormat} to ${this.toDateNumericFormat}`;
},
completelyProcessed() {
return this.rewardReport?.completelyProcessed;
},
Expand Down Expand Up @@ -335,7 +352,7 @@ export default {
},
exportFileLink() {
return this.$rewardService.exportXlsxUrl({periodId: this.period.id,
status: this.status, fileName: 'rewards-detail'});
status: this.status, fileName: this.fileName});
},
},
watch: {
Expand Down