edit referrals actions
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
import { getSessionData } from '@/app/actions/session';
|
||||
import { API_BASE_URL, bankCardFormSchema } from '@/app/actions/definitions';
|
||||
import { FormState } from '@/app/actions/auth';
|
||||
|
||||
export async function fetchReferralsLevels() {
|
||||
const token = await getSessionData('token');
|
||||
@@ -174,9 +173,52 @@ export async function referralRefill() {
|
||||
}
|
||||
}
|
||||
|
||||
export interface PayoutMethods {
|
||||
cardNumber: string,
|
||||
id: number,
|
||||
type: string,
|
||||
displayName: string,
|
||||
maskedDetails: string,
|
||||
default: boolean,
|
||||
verified: boolean
|
||||
}
|
||||
|
||||
export async function getPayoutMethods(): Promise<{
|
||||
methods: PayoutMethods[],
|
||||
error: string | null
|
||||
}> {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/payouts/user/payout-methods`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`,
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const parsed = await response.json();
|
||||
|
||||
return {
|
||||
methods: parsed,
|
||||
error: null
|
||||
};
|
||||
} else {
|
||||
throw 'error';
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
methods: [],
|
||||
error: 'error'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUserPayments() {
|
||||
const token = await getSessionData('token');
|
||||
console.log('getUserPayments');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/payouts/requests`, {
|
||||
@@ -190,7 +232,6 @@ export async function getUserPayments() {
|
||||
|
||||
if (response.ok) {
|
||||
const parsed = await response.json();
|
||||
console.log(parsed);
|
||||
|
||||
return {
|
||||
payments: parsed,
|
||||
@@ -220,6 +261,7 @@ export async function addPayoutMethod(
|
||||
formData: FormData,
|
||||
): Promise<PayoutMethodFormState> {
|
||||
const token = await getSessionData('token');
|
||||
console.log('addPayoutMethod');
|
||||
|
||||
const bankCard = formData.get('bankCard') as string || '';
|
||||
const paymentsType = formData.get('paymentsType') as 'bank-card' | 'bank-transfer' | 'on-account-balance';
|
||||
@@ -250,14 +292,6 @@ export async function addPayoutMethod(
|
||||
};
|
||||
}
|
||||
|
||||
/* return {
|
||||
previousState: {
|
||||
bankCard,
|
||||
paymentsType
|
||||
},
|
||||
error: null
|
||||
}; */
|
||||
|
||||
try {
|
||||
const { bankCard } = validatedFields.data;
|
||||
|
||||
@@ -305,4 +339,76 @@ export async function addPayoutMethod(
|
||||
error: errors
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export type PayoutRequest = {
|
||||
previousState: {
|
||||
amount?: string | undefined;
|
||||
} | undefined,
|
||||
error: string | null
|
||||
};
|
||||
|
||||
export async function createPayoutRequest(
|
||||
state: PayoutRequest | undefined,
|
||||
formData: FormData,
|
||||
): Promise<PayoutRequest> {
|
||||
const token = await getSessionData('token');
|
||||
const amount = formData.get('amount') as string || '';
|
||||
const payoutMethodId = formData.get('payoutMethodId') as string || '';
|
||||
console.log('createPayoutRequest');
|
||||
console.log(token);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/payouts/create-request`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
"amount": amount,
|
||||
"payoutMethodId": payoutMethodId
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${token}`,
|
||||
}
|
||||
});
|
||||
|
||||
console.log(response);
|
||||
|
||||
if (response.status === 400) {
|
||||
const responseText = await response.text();
|
||||
throw JSON.parse(responseText)?.message;
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
const parsed = await response.json();
|
||||
console.log(parsed);
|
||||
|
||||
return {
|
||||
previousState: {
|
||||
amount
|
||||
},
|
||||
error: null
|
||||
};
|
||||
} else {
|
||||
throw 'error-request';
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
|
||||
if (error) {
|
||||
const typedError = error as string;
|
||||
|
||||
return {
|
||||
previousState: {
|
||||
amount
|
||||
},
|
||||
error: typedError
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
previousState: {
|
||||
amount
|
||||
},
|
||||
error: 'error-request'
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user