Skip to content

Commit

Permalink
Implement organization mock in query
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloamorimbr committed Oct 28, 2024
1 parent 8f0b4a9 commit 78d2cd7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 43 deletions.
64 changes: 56 additions & 8 deletions jsapp/js/account/stripe.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {when} from 'mobx';
import subscriptionStore from 'js/account/subscriptionStore';
import {endpoints} from 'js/api.endpoints';
import {ACTIVE_STRIPE_STATUSES} from 'js/constants';
import type {PaginatedResponse} from 'js/dataInterface';
import type {PaginatedResponse, AccountResponse} from 'js/dataInterface';
import envStore from 'js/envStore';
import {fetchGet, fetchPost} from 'jsapp/js/api';
import type {
Expand All @@ -17,6 +17,8 @@ import {Limits} from 'js/account/stripe.types';
import {getAdjustedQuantityForPrice} from 'js/account/stripe.utils';
import {useQuery} from '@tanstack/react-query';
import {QueryKeys} from 'js/query/queryKeys';
import {FeatureFlag, useFeatureFlag} from '../featureFlags';
import sessionStore from 'js/stores/session';

const DEFAULT_LIMITS: AccountLimit = Object.freeze({
submission_limit: Limits.unlimited,
Expand Down Expand Up @@ -48,13 +50,59 @@ export async function changeSubscription(
});
}

export const useOrganizationQuery = () => useQuery({
queryFn: async () => {
const response = await fetchGet<PaginatedResponse<Organization>>(endpoints.ORGANIZATION_URL);
return response.results?.[0];
},
queryKey: [QueryKeys.organization],
});
export const useOrganizationQuery = () => {
const isMmoEnabled = useFeatureFlag(FeatureFlag.mmosEnabled);

// Using a separated function to fetch the organization data to prevent
// feature flag dependencies from being added to the hook
const fetchOrganization = async (): Promise<Organization> => {
// Once we have the full data from backend, this function should be modified to return
// the actual data.

if (isMmoEnabled) {

const currentAccount = sessionStore.currentAccount as AccountResponse;

if (!currentAccount) {throw new Error('No account data found');}

const organizationUrl = currentAccount.organization?.url;

// Here we would fetch organization data from organizationSlug
// For now, we will return mocked data
console.log('Fetching organization data from:', organizationUrl);

// Return mocked data for MMO
return {
name: 'Test Organization',
created: '2024-01-01',
modified: '2024-01-02',
id: '123',
is_active: true,
is_owner: true,
slug: '/test-organization',
is_mmo: true,
request_user_role: 'owner',
};
}

const response = await fetchGet<PaginatedResponse<Organization>>(
endpoints.ORGANIZATION_URL
);
const organization = response.results?.[0];

// Lets 'remove' the MMO data to prevent uncontrolled situations while in development
return {
...organization,
is_mmo: false,
request_user_role: '',
};
};

return useQuery({
queryFn: fetchOrganization,
queryKey: [QueryKeys.organization],
});
};

/**
* Start a checkout session for the given price and organization. Response contains the checkout URL.
Expand Down
35 changes: 0 additions & 35 deletions jsapp/js/query/queries/organizationQuery.ts

This file was deleted.

0 comments on commit 78d2cd7

Please sign in to comment.