add remove method for aliases

This commit is contained in:
2023-07-30 00:50:42 +03:00
parent fb29813345
commit 70d95be227
23 changed files with 446 additions and 376 deletions

View File

@@ -1,92 +1,123 @@
import type { Params, ServiceInterface } from '@feathersjs/feathers'
import type {
NullableId,
Params,
ServiceInterface,
} from "@feathersjs/feathers";
import type { Application } from '../../declarations'
import wildDuckClient from '../../clients/wildduck.client'
import { faker } from '@faker-js/faker'
import { BadRequest } from '@feathersjs/errors'
import config from 'config'
import type { Application } from "../../declarations";
import wildDuckClient from "../../clients/wildduck.client";
import { faker } from "@faker-js/faker";
import { BadRequest } from "@feathersjs/errors";
import config from "config";
interface Alias {
success: boolean,
id: string,
address: string,
main: boolean,
user: string,
tags: string[],
created: string,
success: boolean;
id: string;
address: string;
main: boolean;
user: string;
tags: string[];
created: string;
}
interface GetAddressInfoResponse {
success: boolean,
results: Alias[]
success: boolean;
results: Alias[];
}
interface CreateAddressResponse {
success: boolean,
id: string,
success: boolean;
id: string;
}
type AliasesData = any
type AliasesPatch = any
type AliasesQuery = any
type AliasesData = any;
type AliasesPatch = any;
type AliasesQuery = any;
export type { Alias as Aliases, AliasesData, AliasesPatch, AliasesQuery }
export type { Alias as Aliases, AliasesData, AliasesPatch, AliasesQuery };
export interface AliasesServiceOptions {
app: Application
app: Application;
}
export interface AliasesParams extends Params<AliasesQuery> {
session?: any
session?: any;
}
export class AliasesService<ServiceParams extends AliasesParams = AliasesParams>
implements ServiceInterface<Alias, AliasesData, ServiceParams, AliasesPatch>
{
constructor(public options: AliasesServiceOptions) { }
constructor(public options: AliasesServiceOptions) {}
async find(params: ServiceParams): Promise<Alias[]> {
const userId = await this.getUserIdByEmailAddress(params)
const { data: userAddressesResponse } = await wildDuckClient.get<GetAddressInfoResponse>(`/users/${userId}/addresses`)
const userId = await this.getUserIdByEmailAddress(params);
return userAddressesResponse.results
return this.getUserAddresses(userId);
}
async create(data: AliasesData, params: ServiceParams): Promise<Alias>
async create(data: AliasesData, params: ServiceParams): Promise<Alias | Alias[]> {
const userId = await this.getUserIdByEmailAddress(params)
const aliasFirstPart = faker.animal.crocodilia()
.replace(/\D/g, '')
.replace(/\s/g, '')
async create(data: AliasesData, params: ServiceParams): Promise<Alias>;
async create(
data: AliasesData,
params: ServiceParams,
): Promise<Alias | Alias[]> {
const userId = await this.getUserIdByEmailAddress(params);
const aliasFirstPart = faker.animal
.crocodilia()
.replace(/\D/g, "")
.replace(/\s/g, "")
.slice(0, 10);
const aliasSecondPart = faker.git.commitSha({ length: 5 });
const alias = `${aliasFirstPart}-${aliasSecondPart}@${config.get('wildDuck.domain')}`;
// const alias = `${faker.animal.crocodilia().replace(/\s/, '').slice(10)}-${faker.git.commitSha({ length: 5 })}`;
const alias = `${aliasFirstPart}-${aliasSecondPart}@${config.get(
"wildDuck.domain",
)}`;
const createResult = await wildDuckClient.post<CreateAddressResponse>(`/users/${userId}/addresses`, {
address: alias
})
const createResult = await wildDuckClient.post<CreateAddressResponse>(
`/users/${userId}/addresses`,
{
address: alias,
},
);
if (!createResult.data.success) {
throw new BadRequest('Failed to create alias')
throw new BadRequest("Failed to create alias");
}
const { data: userAddressesResponse } = await wildDuckClient.get<GetAddressInfoResponse>(`/users/${userId}/addresses`)
return userAddressesResponse.results
return this.getUserAddresses(userId);
}
private async getUserIdByEmailAddress(params: ServiceParams): Promise<string> {
private async getUserIdByEmailAddress(
params: ServiceParams,
): Promise<string> {
const emails = params.session?.user?.emails;
const addressInfoResponse = await Promise.any(emails.map((email: string) => wildDuckClient.get<Alias>(`addresses/resolve/${email}`)))
const addressInfoResponse = await Promise.any(
emails.map((email: string) =>
wildDuckClient.get<Alias>(`addresses/resolve/${email}`),
),
);
return addressInfoResponse.data.user
return addressInfoResponse.data.user;
}
private async getUserAddresses(userId: string): Promise<Alias[]> {
const { data: userAddressesResponse } =
await wildDuckClient.get<GetAddressInfoResponse>(
`/users/${userId}/addresses`,
);
return userAddressesResponse.results;
}
async remove(id: NullableId, params: ServiceParams): Promise<Alias[]> {
await wildDuckClient.delete<Alias>(`/addresses/${id}`);
const userId = await this.getUserIdByEmailAddress(params);
return this.getUserAddresses(userId);
}
}
export const getOptions = (app: Application) => {
return { app }
}
return { app };
};