Files
n8n-enterprise-unlocked/packages/cli/src/controllers/userSettings.controller.ts
Mutasem Aldmour 50bd5b9080 feat: Update NPS Value Survey (#9638)
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
Co-authored-by: Tomi Turtiainen <10324676+tomi@users.noreply.github.com>
2024-06-11 10:23:30 +02:00

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,
});
}
}