48 lines
2.1 KiB
TypeScript
48 lines
2.1 KiB
TypeScript
import { ResetPasswordViewDataBuilder } from '@/lib/builders/view-data/ResetPasswordViewDataBuilder';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { PageQuery } from '@/lib/contracts/page-queries/PageQuery';
|
|
import { SearchParamParser } from '@/lib/routing/search-params/SearchParamParser';
|
|
import { AuthPageService } from '@/lib/services/auth/AuthPageService';
|
|
import { ResetPasswordViewData } from '@/lib/view-data/ResetPasswordViewData';
|
|
|
|
export class ResetPasswordPageQuery implements PageQuery<ResetPasswordViewData, URLSearchParams | Record<string, string | string[] | undefined>> {
|
|
private readonly authService: AuthPageService;
|
|
|
|
constructor(authService?: AuthPageService) {
|
|
this.authService = authService || new AuthPageService();
|
|
}
|
|
|
|
async execute(searchParams: URLSearchParams | Record<string, string | string[] | undefined>): Promise<Result<ResetPasswordViewData, string>> {
|
|
// Parse and validate search parameters
|
|
const parsedResult = SearchParamParser.parseAuth(searchParams);
|
|
if (parsedResult.isErr()) {
|
|
return Result.err(`Invalid search parameters: ${parsedResult.getError()}`);
|
|
}
|
|
|
|
const { returnTo, token } = parsedResult.unwrap();
|
|
|
|
try {
|
|
// Use service to process parameters
|
|
const authService = this.authService;
|
|
const serviceResult = await authService.processResetPasswordParams({ returnTo, token });
|
|
|
|
if (serviceResult.isErr()) {
|
|
return Result.err(serviceResult.getError().message);
|
|
}
|
|
|
|
// Transform to ViewData using builder
|
|
const viewData = ResetPasswordViewDataBuilder.build(serviceResult.unwrap());
|
|
return Result.ok(viewData);
|
|
} catch (error: unknown) {
|
|
const message = error instanceof Error ? error.message : 'Failed to execute reset password page query';
|
|
return Result.err(message);
|
|
}
|
|
}
|
|
|
|
// Static factory method for convenience
|
|
static async execute(searchParams: URLSearchParams | Record<string, string | string[] | undefined>): Promise<Result<ResetPasswordViewData, string>> {
|
|
const query = new ResetPasswordPageQuery();
|
|
return query.execute(searchParams);
|
|
}
|
|
}
|