import { ValidationError } from '../errors'; const reasonForChangePropertyOnly = [ 'concurrency-detected', 'b2b-account-created', 'passwordless-signup', 'sso-auto-link' ]; const requiresAllProperties = [ 'masquerader-requested', 'owner-requested', 'reset-password' ]; interface ReasonForChangeOnly { reasonForChange: string; } interface ChangePasswordBody { reasonForChange: string; password: string; oldPassword?: string; token?: string; } /** Class representing a ChangePassword body. */ export class ChangePassword { public reasonForChange: string; public password?: string; public oldPassword?: string; public token?: string; public body: ReasonForChangeOnly | ChangePasswordBody; constructor(reasonForChange: string, password?: string, oldPassword?: string, token?: string) { this.reasonForChange = reasonForChange; this.password = password; this.oldPassword = oldPassword; this.token = token; this.body = this.getBody(); } public getBody (): ReasonForChangeOnly | ChangePasswordBody { if (reasonForChangePropertyOnly.includes(this.reasonForChange)) { return { reasonForChange: this.reasonForChange }; } if (requiresAllProperties.includes(this.reasonForChange)) { if (!this.password || (!this.oldPassword && !this.token )) { throw new ValidationError(`In order to change a user\'s password, with the reason for change of ${this.reasonForChange}, you need a password AND (oldPassword OR token) properties`); } else { return { reasonForChange: this.reasonForChange, password: this.password, token: this.token, oldPassword: this.oldPassword }; } } throw new ValidationError(`Reason for change of ${this.reasonForChange} is not found`); } }