[raffle] Add functionality to reset a raffle

in order to re-use the same raffle by wiping the entrants, resetting
time-fields to their default value and resetting status to draft

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-12-09 16:22:00 +01:00
parent e21fd41e49
commit 6455b409ce
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
3 changed files with 80 additions and 0 deletions

View file

@ -117,6 +117,25 @@ var apiRoutes = []plugins.HTTPRouteRegistrationArgs{
}, },
}, },
{
Description: "Resets the raffle (remove entries, reset status & start/close time) given by its ID",
HandlerFunc: handleWrap(func(w http.ResponseWriter, r *http.Request, ids map[string]uint64) (any, error) {
return nil, errors.Wrap(dbc.Reset(ids["id"]), "resetting raffle")
}, []string{"id"}),
Method: http.MethodPut,
Module: moduleName,
Name: "Reset Raffle",
Path: "/{id}/reset",
RequiresWriteAuth: true,
ResponseType: plugins.HTTPRouteResponseTypeNo200,
RouteParams: []plugins.HTTPRouteParamDocumentation{
{
Description: "ID of the raffle to reset",
Name: "id",
},
},
},
{ {
Description: "Duplicates the raffle given by its ID", Description: "Duplicates the raffle given by its ID",
HandlerFunc: handleWrap(func(w http.ResponseWriter, r *http.Request, ids map[string]uint64) (any, error) { HandlerFunc: handleWrap(func(w http.ResponseWriter, r *http.Request, ids map[string]uint64) (any, error) {

View file

@ -194,6 +194,7 @@ func (d *dbClient) Clone(raffleID uint64) error {
return errors.Wrap(err, "getting raffle") return errors.Wrap(err, "getting raffle")
} }
raffle.AutoStartAt = nil
raffle.CloseAt = nil raffle.CloseAt = nil
raffle.Entries = nil raffle.Entries = nil
raffle.ID = 0 raffle.ID = 0
@ -528,6 +529,33 @@ func (d *dbClient) Reopen(raffleID uint64, duration time.Duration) error {
return nil return nil
} }
// Reset resets the raffle participants, the status, start / close
// times while preserving the rest of the settings
func (d *dbClient) Reset(raffleID uint64) error {
raffle, err := d.Get(raffleID)
if err != nil {
return errors.Wrap(err, "getting raffle")
}
raffle.AutoStartAt = nil
raffle.CloseAt = nil
raffle.Entries = nil
raffle.Status = raffleStatusPlanned
if err = helpers.RetryTransaction(d.db.DB(), func(tx *gorm.DB) error {
if err = tx.Delete(&raffleEntry{}, "raffle_id = ?", raffleID).Error; err != nil {
return errors.Wrap(err, "deleting raffle entries")
}
return tx.Save(raffle).Error
}); err != nil {
return errors.Wrap(err, "saving cleaned raffle")
}
frontendNotify(frontendNotifyEventRaffleChange)
return nil
}
// Start fetches the given raffle, updates its CloseAt attribute // Start fetches the given raffle, updates its CloseAt attribute
// in case it is not already set, sets the raffle to active, updates // in case it is not already set, sets the raffle to active, updates
// the raffle in the database and notes its channel/keyword combo // the raffle in the database and notes its channel/keyword combo

View file

@ -79,6 +79,18 @@
/> />
</b-button> </b-button>
<b-button
variant="warning"
:disabled="data.item.status !== 'ended'"
title="Reset Raffle"
@click="resetRaffle(data.item.id)"
>
<font-awesome-icon
fixed-width
:icon="['fas', 'recycle']"
/>
</b-button>
<b-button <b-button
variant="danger" variant="danger"
title="Delete Raffle" title="Delete Raffle"
@ -977,6 +989,27 @@ export default {
.catch(() => this.$root.toastError('Could not re-pick winner!')) .catch(() => this.$root.toastError('Could not re-pick winner!'))
}, },
resetRaffle(id) {
this.$bvModal.msgBoxConfirm('Do you really want to reset this raffle?', {
buttonSize: 'sm',
cancelTitle: 'NO',
centered: true,
okTitle: 'YES',
okVariant: 'danger',
size: 'sm',
title: 'Please Confirm',
})
.then(val => {
if (!val) {
return
}
return axios.put(`raffle/${id}/reset`, {}, this.$root.axiosOptions)
.then(() => this.$root.toastSuccess('Raffle reset'))
.catch(err => this.$bus.$emit(constants.NOTIFY_FETCH_ERROR, err))
})
},
saveRaffle() { saveRaffle() {
if (this.models.raffle.id) { if (this.models.raffle.id) {
return axios.put(`raffle/${this.models.raffle.id}`, this.transformRaffleToDB(this.models.raffle), this.$root.axiosOptions) return axios.put(`raffle/${this.models.raffle.id}`, this.transformRaffleToDB(this.models.raffle), this.$root.axiosOptions)