Skip to content

Commit

Permalink
feat: Disable status update once reward sent (accepted/rejected) - ME…
Browse files Browse the repository at this point in the history
…ED-3980 - Meeds-io/MIPs#122 (#511)

This PR will ensure disabling the update achievement status once the
reward is sent.
  • Loading branch information
AzmiTouil authored and exo-swf committed Apr 16, 2024
1 parent 9aba1f7 commit 7069e85
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,27 @@ public Response countRewards(@Context Request request, @Parameter(description =
}
}

@GET
@Path("periods")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed("rewarding")
@Operation(summary = "Retrieves the list of periods sorted descending by start date", method = "GET", description = "returns the list of periods sorted descending by start date")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request fulfilled"),
@ApiResponse(responseCode = "400", description = "Invalid query input"),
@ApiResponse(responseCode = "401", description = "Unauthorized operation"),
@ApiResponse(responseCode = "500", description = "Internal server error") })
public Response getRewardReportPeriods(@Parameter(description = "Offset of results to retrieve") @QueryParam("from") @DefaultValue("0") long from,
@Parameter(description = "Offset of results to retrieve") @QueryParam("to") @DefaultValue("0") long to,
@Parameter(description = "Offset of results to retrieve") @QueryParam("offset") @DefaultValue("0") int offset,
@Parameter(description = "Limit of results to retrieve") @QueryParam("limit") @DefaultValue("0") int limit) {
if (offset < 0) {
return Response.status(Response.Status.BAD_REQUEST).entity("Offset must be 0 or positive").build();
}
List<RewardPeriod> rewardPeriods = rewardReportService.findRewardPeriodsBetween(from, to, offset, limit);
return Response.ok(rewardPeriods).build();
}

private RewardPeriod getRewardPeriod(String date) {
RewardSettings settings = rewardSettingsService.getSettings();
ZoneId zoneId = settings.zoneId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ public interface RewardReportService {
*/
List<RewardPeriod> findRewardReportPeriods(int offset, int limit);

/**
* Retrieves the list of periods by interval sorted descending by start date
*
* @param from from date
* @param to to date
* @param offset offset of query
* @param limit limit of results
* @return {@link List} of {@link RewardPeriod}
*/
List<RewardPeriod> findRewardPeriodsBetween(long from, long to, int offset, int limit);

/**
* @param currentUser current user listing his rewards
* @param limit size limit of items to return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public List<WalletRewardPeriodEntity> findRewardPeriods(int offset, int limit) {
return CollectionUtils.isEmpty(result) ? Collections.emptyList() : result;
}

public List<WalletRewardPeriodEntity> findRewardPeriodsBetween(long from, long to, int offset, int limit) {
TypedQuery<WalletRewardPeriodEntity> query = getEntityManager().createNamedQuery("RewardPeriod.findRewardPeriodsBetween",
WalletRewardPeriodEntity.class);
if (offset > 0) {
query.setFirstResult(offset);
}
if (limit > 0) {
query.setMaxResults(limit);
}
query.setParameter("from", from);
query.setParameter("to", to);
List<WalletRewardPeriodEntity> result = query.getResultList();
return CollectionUtils.isEmpty(result) ? Collections.emptyList() : result;
}

public WalletRewardPeriodEntity findRewardPeriodByTypeAndTime(RewardPeriodType periodType, long periodTime) {
TypedQuery<WalletRewardPeriodEntity> query = getEntityManager().createNamedQuery("RewardPeriod.findRewardPeriodByTypeAndTime",
WalletRewardPeriodEntity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@DynamicUpdate
@Table(name = "ADDONS_WALLET_REWARD_PERIOD")
@NamedQuery(name = "RewardPeriod.findRewardPeriods", query = "SELECT rp FROM RewardPeriod rp ORDER BY rp.startTime DESC")
@NamedQuery(name = "RewardPeriod.findRewardPeriodsBetween", query = "SELECT rp FROM RewardPeriod rp WHERE (rp.startTime >= :from AND rp.startTime <= :to) OR (rp.endTime >= :from AND rp.endTime <= :to) ORDER BY rp.startTime DESC")
@NamedQuery(name = "RewardPeriod.findRewardPeriodByTypeAndTime", query = "SELECT rp FROM RewardPeriod rp WHERE rp.periodType = :periodType AND rp.startTime <= :periodTime AND rp.endTime > :periodTime")
@NamedQuery(name = "RewardPlugin.findRewardPeriodsByStatus", query = "SELECT rp FROM RewardPeriod rp WHERE rp.status = :status")
public class WalletRewardPeriodEntity implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ public List<RewardPeriod> findRewardReportPeriods(int offset, int limit) {
return rewardReportStorage.findRewardReportPeriods(offset, limit);
}

@Override
public List<RewardPeriod> findRewardPeriodsBetween(long from, long to, int offset, int limit) {
return rewardReportStorage.findRewardPeriodsBetween(from, to, offset, limit);
}

@Override
public List<RewardPeriod> getRewardPeriodsInProgress() {
return rewardReportStorage.findRewardPeriodsByStatus(RewardStatus.PENDING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public List<RewardPeriod> findRewardReportPeriods(int offset, int limit) {
return rewardPeriodEntities.stream().map(this::toDTO).toList();
}

public List<RewardPeriod> findRewardPeriodsBetween(long from, long to, int offset, int limit) {
List<WalletRewardPeriodEntity> rewardPeriodEntities = rewardPeriodDAO.findRewardPeriodsBetween(from, to, offset, limit);
return rewardPeriodEntities.stream().map(this::toDTO).toList();
}

public RewardReport getRewardReportByPeriodId(long id, ZoneId zoneId) {
return getRewardReport(rewardPeriodDAO.find(id), zoneId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import org.exoplatform.wallet.utils.RewardUtils;
import org.exoplatform.wallet.utils.WalletUtils;

import static org.exoplatform.wallet.utils.RewardUtils.timeToSecondsAtDayStart;

public class WalletRewardReportServiceTest extends BaseWalletRewardTest { // NOSONAR

/**
Expand Down Expand Up @@ -712,6 +714,12 @@ public void testFindRewardReportPeriods() {
List<RewardPeriod> rewardReportPeriods = rewardReportService.findRewardReportPeriods(0, 1);
assertNotNull(rewardReportPeriods);
assertEquals(1, rewardReportPeriods.size());

long from = timeToSecondsAtDayStart(LocalDate.now(), ZoneId.systemDefault());
long to = timeToSecondsAtDayStart(LocalDate.now().plusMonths(1), ZoneId.systemDefault());
rewardReportPeriods = rewardReportService.findRewardPeriodsBetween(from, to, 0, 10);
assertNotNull(rewardReportPeriods);
assertEquals(1, rewardReportPeriods.size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ gamification.event.title.createWallet=Wallet: Initialize the wallet
gamification.event.description.createWallet=You Initialize your wallet

gamification.event.display.yourWallet=Your Wallet
gamification.achievement.cannotUpdateStatus.tooltip=Status cannot be updated as reward have been sent
23 changes: 23 additions & 0 deletions wallet-webapps/src/main/webapp/WEB-INF/gatein-resources.xml
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,29 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
</depends>
</module>

<module>
<name>engagementCenterAchievementsWalletExtensions</name>
<load-group>engagement-center-achievements-extensions</load-group>
<script>
<path>/js/achievementsExtensions.bundle.js</path>
</script>
<depends>
<module>vue</module>
</depends>
<depends>
<module>vuetify</module>
</depends>
<depends>
<module>eXoVueI18n</module>
</depends>
<depends>
<module>extensionRegistry</module>
</depends>
<depends>
<module>commonVueComponents</module>
</depends>
</module>

<module>
<name>walletNotificationExtension</name>
<load-group>notificationGRP</load-group>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association contact@meeds.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
import {getRewardReportPeriods} from './js/service';

export function init() {
extensionRegistry.registerExtension('engagementCenterAchievements', 'achievements-extensions', {
type: 'wallet',
rewardPeriods: [],
init(from, to) {
getRewardReportPeriods(from, to, 0, -1).then(period => {
this.rewardPeriods = period;
});
},
canUpdateStatus(createdDate) {
return this.rewardPeriods.filter(rewardPeriod => this.isInPeriod(rewardPeriod, createdDate)).length === 0;
},
isInPeriod(period, timestamp) {
return timestamp >= period?.startDateInSeconds && timestamp <= period?.endDateInSeconds;
},
cannotUpdateStatusLabel: 'gamification.achievement.cannotUpdateStatus.tooltip',
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association contact@meeds.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
export function getRewardReportPeriods(from, to, offset, limit) {
return fetch(`/portal/rest/wallet/api/reward/periods?from=${from}&to=${to}&offset=${offset || 0}&limit=${limit|| 10}`, {
method: 'GET',
credentials: 'include',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}).then((resp) => {
if (resp && resp.ok) {
return resp.json();
} else {
throw new Error ('Error computing rewards');
}
});
}
1 change: 1 addition & 0 deletions wallet-webapps/webpack.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const config = merge(webpackCommonConfig, {
rewardApp: './src/main/webapp/vue-app/wallet-reward/main.js',
engagementCenterExtensions: './src/main/webapp/vue-app/engagementCenterExtensions/extensions.js',
connectorEventExtensions: './src/main/webapp/vue-app/connectorEventExtensions/extensions.js',
achievementsExtensions: './src/main/webapp/vue-app/achievementsExtensions/extensions.js',
notificationExtension: './src/main/webapp/vue-app/notification-extension/main.js',
},
output: {
Expand Down

0 comments on commit 7069e85

Please sign in to comment.