mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 11:01:15 +00:00
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in> Co-authored-by: Tomi Turtiainen <10324676+tomi@users.noreply.github.com>
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { Patch, RestController } from '@/decorators';
|
|
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
|
import { NpsSurveyRequest } from '@/requests';
|
|
import { UserService } from '@/services/user.service';
|
|
import type { NpsSurveyState } from 'n8n-workflow';
|
|
|
|
function getNpsSurveyState(state: unknown): NpsSurveyState | undefined {
|
|
if (typeof state !== 'object' || state === null) {
|
|
return;
|
|
}
|
|
if (!('lastShownAt' in state) || typeof state.lastShownAt !== 'number') {
|
|
return;
|
|
}
|
|
if ('responded' in state && state.responded === true) {
|
|
return {
|
|
responded: true,
|
|
lastShownAt: state.lastShownAt,
|
|
};
|
|
}
|
|
|
|
if (
|
|
'waitingForResponse' in state &&
|
|
state.waitingForResponse === true &&
|
|
'ignoredCount' in state &&
|
|
typeof state.ignoredCount === 'number'
|
|
) {
|
|
return {
|
|
waitingForResponse: true,
|
|
ignoredCount: state.ignoredCount,
|
|
lastShownAt: state.lastShownAt,
|
|
};
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
@RestController('/user-settings')
|
|
export class UserSettingsController {
|
|
constructor(private readonly userService: UserService) {}
|
|
|
|
@Patch('/nps-survey')
|
|
async updateNpsSurvey(req: NpsSurveyRequest.NpsSurveyUpdate): Promise<void> {
|
|
const state = getNpsSurveyState(req.body);
|
|
if (!state) {
|
|
throw new BadRequestError('Invalid nps survey state structure');
|
|
}
|
|
|
|
await this.userService.updateSettings(req.user.id, {
|
|
npsSurvey: state,
|
|
});
|
|
}
|
|
}
|