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 (#509)

This PR will ensure disabling the update achievement status once the
reward is sent.
  • Loading branch information
AzmiTouil authored Mar 26, 2024
1 parent f20c5d9 commit 7358781
Show file tree
Hide file tree
Showing 11 changed files with 155 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.findRewardPeriodsByInterval(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> findRewardPeriodsByInterval(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> findRewardPeriodsByInterval(long from, long to, int offset, int limit) {
TypedQuery<WalletRewardPeriodEntity> query = getEntityManager().createNamedQuery("RewardPeriod.findRewardPeriodsByInterval",
WalletRewardPeriodEntity.class);
if (offset > 0) {
query.setFirstResult(offset);
}
if (limit > 0) {
query.setMaxResults(limit);
}
query.setParameter("startDate", from);
query.setParameter("endDate", 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.findRewardPeriodsByInterval", query = "SELECT rp FROM RewardPeriod rp WHERE (rp.startTime > :startDate AND rp.startTime < :endDate) OR (rp.endTime > :startDate AND rp.endTime < :endDate) 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> findRewardPeriodsByInterval(long from, long to, int offset, int limit) {
return rewardReportStorage.findRewardPeriodsByInterval(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> findRewardPeriodsByInterval(long from, long to, int offset, int limit) {
List<WalletRewardPeriodEntity> rewardPeriodEntities = rewardPeriodDAO.findRewardPeriodsByInterval(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 @@ -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 7358781

Please sign in to comment.