Skip to content

Commit

Permalink
fix: support billing
Browse files Browse the repository at this point in the history
  • Loading branch information
qinluhe committed Dec 26, 2024
1 parent 90fda98 commit ad34586
Show file tree
Hide file tree
Showing 90 changed files with 1,905 additions and 779 deletions.
2 changes: 1 addition & 1 deletion frontend/appflowy_web_app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"coverage": "pnpm run test:unit && pnpm run test:components"
},
"dependencies": {
"@appflowyinc/editor": "^0.0.30",
"@appflowyinc/editor": "^0.0.39",
"@atlaskit/primitives": "^5.5.3",
"@emoji-mart/data": "^1.1.2",
"@emoji-mart/react": "^1.1.1",
Expand Down
76 changes: 10 additions & 66 deletions frontend/appflowy_web_app/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import axios, { AxiosInstance } from 'axios';

let axiosInstance: AxiosInstance | null = null;

export function initGrantService (baseURL: string) {
export function initGrantService(baseURL: string) {
if (axiosInstance) {
return;
}
Expand All @@ -21,7 +21,7 @@ export function initGrantService (baseURL: string) {
});
}

export async function refreshToken (refresh_token: string) {
export async function refreshToken(refresh_token: string) {
const response = await axiosInstance?.post<{
access_token: string;
expires_at: number;
Expand All @@ -34,12 +34,14 @@ export async function refreshToken (refresh_token: string) {

if (newToken) {
refreshSessionToken(JSON.stringify(newToken));
} else {
return Promise.reject('Failed to refresh token');
}

return newToken;
}

export async function signInWithMagicLink (email: string, authUrl: string) {
export async function signInWithMagicLink(email: string, authUrl: string) {
const res = await axiosInstance?.post(
'/magiclink',
{
Expand All @@ -58,13 +60,13 @@ export async function signInWithMagicLink (email: string, authUrl: string) {
return res?.data;
}

export async function settings () {
export async function settings() {
const res = await axiosInstance?.get('/settings');

return res?.data;
}

export function signInGoogle (authUrl: string) {
export function signInGoogle(authUrl: string) {
const provider = 'google';
const redirectTo = encodeURIComponent(authUrl);
const accessType = 'offline';
Expand All @@ -75,7 +77,7 @@ export function signInGoogle (authUrl: string) {
window.open(url, '_current');
}

export function signInApple (authUrl: string) {
export function signInApple(authUrl: string) {
const provider = 'apple';
const redirectTo = encodeURIComponent(authUrl);
const baseURL = axiosInstance?.defaults.baseURL;
Expand All @@ -84,7 +86,7 @@ export function signInApple (authUrl: string) {
window.open(url, '_current');
}

export function signInGithub (authUrl: string) {
export function signInGithub(authUrl: string) {
const provider = 'github';
const redirectTo = encodeURIComponent(authUrl);
const baseURL = axiosInstance?.defaults.baseURL;
Expand All @@ -93,7 +95,7 @@ export function signInGithub (authUrl: string) {
window.open(url, '_current');
}

export function signInDiscord (authUrl: string) {
export function signInDiscord(authUrl: string) {
const provider = 'discord';
const redirectTo = encodeURIComponent(authUrl);
const baseURL = axiosInstance?.defaults.baseURL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import axios, { AxiosInstance } from 'axios';
import dayjs from 'dayjs';
import { omit } from 'lodash-es';
import { nanoid } from 'nanoid';
import { notify } from '@/components/_shared/notify';

export * from './gotrue';

Expand Down Expand Up @@ -72,9 +73,14 @@ export function initAPIService(config: AFCloudConfig) {
const refresh_token = token.refresh_token;

if (isExpired) {
const newToken = await refreshToken(refresh_token);

access_token = newToken?.access_token || '';
try {
const newToken = await refreshToken(refresh_token);

access_token = newToken?.access_token || '';
} catch (e) {
invalidToken();
return config;
}
}

if (access_token) {
Expand Down Expand Up @@ -1192,6 +1198,18 @@ export async function getSubscriptions() {

}

export async function getWorkspaceSubscriptions(workspaceId: string) {
try {
const plans = await getActiveSubscription(workspaceId);
const subscriptions = await getSubscriptions();

return subscriptions?.filter((subscription) => plans?.includes(subscription.plan));

} catch (e) {
return Promise.reject(e);
}
}

export async function getActiveSubscription(workspaceId: string) {
const url = `/billing/api/v1/active-subscription/${workspaceId}`;

Expand Down Expand Up @@ -1405,32 +1423,62 @@ export async function updateSpace(workspaceId: string, payload: UpdateSpacePaylo
export async function uploadFile(workspaceId: string, viewId: string, file: File, onProgress?: (progress: number) => void) {
const url = `/api/file_storage/${workspaceId}/v1/blob/${viewId}`;

const response = await axiosInstance?.put<{
code: number;
message: string;
data: {
file_id: string;
// Check file size, if over 7MB, check subscription plan
if (file.size > 7 * 1024 * 1024) {
const plan = await getActiveSubscription(workspaceId);

if (plan?.length === 0 || plan?.[0] === SubscriptionPlan.Free) {
notify.error('Your file is over 7 MB limit of the Free plan. Upgrade for unlimited uploads.');

return Promise.reject({
code: 413,
message: 'File size is too large. Please upgrade your plan for unlimited uploads.',
});
}
}>(url, file, {
onUploadProgress: (progressEvent) => {
const { progress = 0 } = progressEvent;
}

onProgress?.(progress);
},
headers: {
'Content-Type': file.type || 'application/octet-stream',
},
});
try {
const response = await axiosInstance?.put<{
code: number;
message: string;
data: {
file_id: string;
}
}>(url, file, {
onUploadProgress: (progressEvent) => {
const { progress = 0 } = progressEvent;

if (response?.data.code === 0) {
const baseURL = axiosInstance?.defaults.baseURL;
const url = `${baseURL}/api/file_storage/${workspaceId}/v1/blob/${viewId}/${response?.data.data.file_id}`;
onProgress?.(progress);
},
headers: {
'Content-Type': file.type || 'application/octet-stream',
},
});

if (response?.data.code === 0) {
const baseURL = axiosInstance?.defaults.baseURL;
const url = `${baseURL}/api/file_storage/${workspaceId}/v1/blob/${viewId}/${response?.data.data.file_id}`;

return url;
}

console.log('Upload file success:', url);
return url;
return Promise.reject(response?.data);
// eslint-disable-next-line
} catch (e: any) {

if (e.response.status === 413) {
return Promise.reject({
code: 413,
message: 'File size is too large. Please upgrade your plan for unlimited uploads.',
});
}
}

return Promise.reject(response?.data);
return Promise.reject({
code: -1,
message: 'Upload file failed.',
});

}

export async function inviteMembers(workspaceId: string, emails: string[]) {
Expand Down Expand Up @@ -1571,5 +1619,24 @@ export async function deleteQuickNote(workspaceId: string, noteId: string) {
return;
}

return Promise.reject(res?.data);
}

export async function cancelSubscription(workspaceId: string, plan: SubscriptionPlan, reason?: string) {
const url = `/billing/api/v1/cancel-subscription`;
const res = await axiosInstance?.post<{
code: number;
message: string;
}>(url, {
workspace_id: workspaceId,
plan,
sync: true,
reason,
});

if (res?.data.code === 0) {
return;
}

return Promise.reject(res?.data);
}
Loading

0 comments on commit ad34586

Please sign in to comment.