Enhance gift sending logic: Implement retry mechanism for 'tooOften' error in FalukantService and update error handling in FamilyView to display retry time.

This commit is contained in:
Torsten Schulz (local)
2026-01-26 16:45:13 +01:00
parent ca614f6cc2
commit 955ea1a9ed
5 changed files with 29 additions and 6 deletions

View File

@@ -99,7 +99,16 @@ class FalukantController {
return this.service.getGifts(userId); return this.service.getGifts(userId);
}); });
this.getChildren = this._wrapWithUser((userId) => this.service.getChildren(userId)); this.getChildren = this._wrapWithUser((userId) => this.service.getChildren(userId));
this.sendGift = this._wrapWithUser((userId, req) => this.service.sendGift(userId, req.body.giftId)); this.sendGift = this._wrapWithUser(async (userId, req) => {
try {
return await this.service.sendGift(userId, req.body.giftId);
} catch (e) {
if (e && e.name === 'PreconditionError' && e.message === 'tooOften') {
throw { status: 412, message: 'tooOften', retryAt: e.meta?.retryAt };
}
throw e;
}
});
this.getTitlesOfNobility = this._wrapWithUser((userId) => this.service.getTitlesOfNobility(userId)); this.getTitlesOfNobility = this._wrapWithUser((userId) => this.service.getTitlesOfNobility(userId));
this.getHouseTypes = this._wrapWithUser((userId) => this.service.getHouseTypes(userId)); this.getHouseTypes = this._wrapWithUser((userId) => this.service.getHouseTypes(userId));

View File

@@ -2868,7 +2868,10 @@ class FalukantService extends BaseService {
limit: 1 limit: 1
}); });
if (lastGift && (lastGift.createdAt.getTime() + 3_600_000) > Date.now()) { if (lastGift && (lastGift.createdAt.getTime() + 3_600_000) > Date.now()) {
throw new PreconditionError('tooOften'); const retryAt = new Date(lastGift.createdAt.getTime() + 3_600_000);
const err = new PreconditionError('tooOften');
err.meta = { retryAt: retryAt.toISOString() };
throw err;
} }
const gift = await PromotionalGift.findOne({ const gift = await PromotionalGift.findOne({
where: { id: giftId }, where: { id: giftId },

View File

@@ -522,7 +522,8 @@
"tooOften": "Du kannst nicht so oft Geschenke machen.", "tooOften": "Du kannst nicht so oft Geschenke machen.",
"insufficientFunds": "Du hast nicht genug Geld." "insufficientFunds": "Du hast nicht genug Geld."
}, },
"success": "Das Geschenk wurde überreicht." "success": "Das Geschenk wurde überreicht.",
"nextGiftAt": "Nächstes Geschenk ab"
} }
}, },
"product": { "product": {

View File

@@ -353,8 +353,18 @@ export default {
this.$root.$refs.messageDialog.open('tr:falukant.family.sendgift.success'); this.$root.$refs.messageDialog.open('tr:falukant.family.sendgift.success');
} catch (error) { } catch (error) {
console.log(error.response); console.log(error.response);
if (error.response.status === 412) { if (error.response?.status === 412) {
const retryAtIso = error.response?.data?.retryAt;
if (retryAtIso) {
const retryStr = new Date(retryAtIso).toLocaleString(navigator.language, {
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit'
});
const baseMsg = this.$t(`falukant.family.sendgift.error.${error.response.data.error}`);
this.$root.$refs.errorDialog.open(`${baseMsg}${this.$t('falukant.family.sendgift.nextGiftAt')}: ${retryStr}`);
} else {
this.$root.$refs.errorDialog.open(`tr:falukant.family.sendgift.error.${error.response.data.error}`); this.$root.$refs.errorDialog.open(`tr:falukant.family.sendgift.error.${error.response.data.error}`);
}
} else { } else {
this.$root.$refs.errorDialog.open(`tr:falukant.family.sendgift.error.generic`); this.$root.$refs.errorDialog.open(`tr:falukant.family.sendgift.error.generic`);
} }