games_app/backend/dist-test-faq/services/supportMessagePolicy.js

67 lines
2.6 KiB
JavaScript

"use strict";
/**
* Pure rules for support message edit/delete windows.
* Window duration comes from system settings; callers enforce ownership separately.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.clampSupportWindowMinutes = clampSupportWindowMinutes;
exports.computeActionWindow = computeActionWindow;
exports.resolveSupportMessageActions = resolveSupportMessageActions;
exports.assertSupportMessageAction = assertSupportMessageAction;
exports.isSupportMessageOwner = isSupportMessageOwner;
function clampSupportWindowMinutes(value) {
if (!Number.isFinite(value))
return 0;
return Math.min(10080, Math.max(0, Math.trunc(value)));
}
function computeActionWindow(createdAt, windowMinutes, now = new Date()) {
const minutes = clampSupportWindowMinutes(windowMinutes);
if (minutes <= 0) {
return { allowed: false, availableUntil: null };
}
const until = new Date(createdAt.getTime() + minutes * 60000);
return { allowed: now.getTime() < until.getTime(), availableUntil: until };
}
function resolveSupportMessageActions(input) {
if (input.deleted || !input.isOwner) {
return {
canEdit: false,
canDelete: false,
editAvailableUntil: null,
deleteAvailableUntil: null,
};
}
const now = input.now ?? new Date();
const edit = computeActionWindow(input.createdAt, input.editWindowMinutes, now);
const del = computeActionWindow(input.createdAt, input.deleteWindowMinutes, now);
return {
canEdit: edit.allowed,
canDelete: del.allowed,
editAvailableUntil: edit.availableUntil?.toISOString() ?? null,
deleteAvailableUntil: del.availableUntil?.toISOString() ?? null,
};
}
function assertSupportMessageAction(action, input) {
const flags = resolveSupportMessageActions(input);
if (action === 'edit' && flags.canEdit)
return flags;
if (action === 'delete' && flags.canDelete)
return flags;
if (!input.isOwner) {
throw new Error('Forbidden');
}
if (input.deleted) {
throw new Error('Message already deleted');
}
throw new Error(action === 'edit' ? 'Edit window expired' : 'Delete window expired');
}
function isSupportMessageOwner(input) {
if (input.senderType === 'staff' || input.senderType === 'user') {
return !!input.actorUserId && input.senderUserId === input.actorUserId;
}
return (input.senderType === 'visitor' &&
!!input.actorVisitorToken &&
!!input.conversationVisitorToken &&
input.actorVisitorToken === input.conversationVisitorToken);
}
//# sourceMappingURL=supportMessagePolicy.js.map