revert Merge pull request 'try to do better' (#18) from refactor/frontend-code-quality into master Reviewed-on: http://192.168.31.100:3847/Arrelin/family_budget/pulls/18
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
import { inviteLinkApi } from '../api/client';
|
|
import { CreateInviteLinkRequest, InviteLinkResponse, ValidateInviteResponse, JoinFamilyResponse } from '../types';
|
|
import { handleApiError } from '../utils/errorHandler';
|
|
|
|
export const inviteService = {
|
|
async create(data: CreateInviteLinkRequest): Promise<InviteLinkResponse> {
|
|
try {
|
|
const res = await inviteLinkApi.create(data);
|
|
return res.data;
|
|
} catch (error) {
|
|
handleApiError(error);
|
|
}
|
|
},
|
|
|
|
async getMyLinks(): Promise<InviteLinkResponse[]> {
|
|
try {
|
|
const res = await inviteLinkApi.getMyLinks();
|
|
return res.data;
|
|
} catch (error) {
|
|
handleApiError(error);
|
|
}
|
|
},
|
|
|
|
async delete(token: string): Promise<void> {
|
|
try {
|
|
await inviteLinkApi.delete(token);
|
|
} catch (error) {
|
|
handleApiError(error);
|
|
}
|
|
},
|
|
|
|
async validate(token: string): Promise<ValidateInviteResponse> {
|
|
try {
|
|
const res = await inviteLinkApi.validate(token);
|
|
return res.data;
|
|
} catch (error) {
|
|
handleApiError(error);
|
|
}
|
|
},
|
|
|
|
async join(token: string): Promise<JoinFamilyResponse> {
|
|
try {
|
|
const res = await inviteLinkApi.join(token);
|
|
return res.data;
|
|
} catch (error) {
|
|
handleApiError(error);
|
|
}
|
|
},
|
|
|
|
isExpired(expiresAt: string | null): boolean {
|
|
if (!expiresAt) return false;
|
|
return new Date(expiresAt) < new Date();
|
|
},
|
|
|
|
isMaxUsesReached(link: InviteLinkResponse): boolean {
|
|
if (link.max_uses === null) return false;
|
|
return link.uses_count >= link.max_uses;
|
|
},
|
|
|
|
isActive(link: InviteLinkResponse): boolean {
|
|
return !this.isExpired(link.expires_at) && !this.isMaxUsesReached(link);
|
|
},
|
|
|
|
formatExpiresAt(expiresAt: string | null): string {
|
|
if (!expiresAt) return 'Never';
|
|
const date = new Date(expiresAt);
|
|
return date.toLocaleString('ru-RU', {
|
|
day: '2-digit',
|
|
month: '2-digit',
|
|
year: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
});
|
|
},
|
|
};
|