Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mai/fix cookie weeklyapi #247

Merged
merged 68 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
9dd85ca
fix: fixed userResults data structure
chris-nowicki May 15, 2024
50632e6
fix: pnpm-lock.yaml
chris-nowicki May 15, 2024
2194f1b
fix: resolving PR comments
chris-nowicki May 15, 2024
55d665a
Merge branch 'develop' into chris/fix-userResult-object
chris-nowicki May 15, 2024
298cd38
fix: all comments but unit and e2e tests
chris-nowicki May 15, 2024
5e9d2ab
fixed dataStore changing from dev branch
chris-nowicki May 15, 2024
b603930
fix: add function to return userSelect object
chris-nowicki May 15, 2024
dc5b2e9
comment
vmaineng May 16, 2024
532f390
started working on unit test updates
chris-nowicki May 16, 2024
f053574
add test for getAllWeeklyPicks
chris-nowicki May 16, 2024
fc29ba4
pulled in from develop
vmaineng May 16, 2024
879d390
Merge branch 'chris/fix-userResult-object' into mai/fix-cookie-weeklyapi
vmaineng May 16, 2024
c394782
updated test variable names
chris-nowicki May 16, 2024
ce79dcf
remove unused import
chris-nowicki May 16, 2024
fe00c51
Merge branch 'chris/fix-userResult-object' into mai/fix-cookie-weeklyapi
vmaineng May 16, 2024
656712c
pulled in new changes
vmaineng May 16, 2024
bdcbaec
adding in chagnes
vmaineng May 16, 2024
4db4e0e
added in update local state for userPick
chris-nowicki May 16, 2024
e8f607d
Merge branch 'chris/fix-userResult-object' into mai/fix-cookie-weeklyapi
vmaineng May 17, 2024
c786501
changes
vmaineng May 17, 2024
b94b314
Merge branch 'mai/fix-cookie-weeklyapi' of github.com:LetsGetTechnica…
vmaineng May 17, 2024
7fe5319
added in new changes
vmaineng May 17, 2024
0aa21ce
leave as string
vmaineng May 17, 2024
6d49b7b
added in updates
vmaineng May 17, 2024
9f89cbc
added in code
vmaineng May 17, 2024
4808408
Merge branch 'develop' into mai/fix-cookie-weeklyapi
vmaineng May 17, 2024
c3413e7
added in new ones
vmaineng May 17, 2024
d3eaed2
release: v0.2.2-alpha
chris-nowicki May 17, 2024
0f26808
fix: did we? no ... but we are close
chris-nowicki May 17, 2024
37330cb
modififed userResults to object
vmaineng May 18, 2024
8543b95
Merge branch 'develop' of github.com:LetsGetTechnical/gridiron-surviv…
chris-nowicki May 18, 2024
e106f93
fix: userResult coming from DB and displaying properly
chris-nowicki May 18, 2024
ae50d83
fix: loading from state userPick
chris-nowicki May 18, 2024
174c28e
fix: userData loaded from state/db and not local storage
chris-nowicki May 19, 2024
2829a77
fix when user pick is empty string
chris-nowicki May 19, 2024
ff1b4d9
fix: check user error when logged out
chris-nowicki May 19, 2024
3743db3
renamed fetchWeeklyPicks to setWeeklyPicks
chris-nowicki May 20, 2024
c0b3b55
added in comments
vmaineng May 20, 2024
0fb21af
added in comments
vmaineng May 20, 2024
42adf65
added my notes
vmaineng May 21, 2024
9a31d5a
Merge branch 'develop' of github.com:LetsGetTechnical/gridiron-surviv…
chris-nowicki May 22, 2024
d4be071
added in notes
vmaineng May 22, 2024
4955103
added in logic
vmaineng May 22, 2024
4f04a5f
added in fetch, state
vmaineng May 22, 2024
c668052
added in notes
vmaineng May 23, 2024
868b512
push changes
vmaineng May 23, 2024
1f69409
added in currentWeek and gameGroup to state
vmaineng May 23, 2024
37369bb
added in getCurrentWeek
vmaineng May 24, 2024
34ec79c
added in last one
vmaineng May 24, 2024
67ba1a5
added in apiFunctions.ts
vmaineng May 24, 2024
3b6efd3
fix: now using dynamic gameId and currentWeekId
chris-nowicki May 25, 2024
412ec76
type updates
chris-nowicki May 25, 2024
82a6af7
fixed type error
chris-nowicki May 25, 2024
cd98680
removed unused types
chris-nowicki May 25, 2024
2945182
moved functions to utils, added caching and page revalidate for cache.
chris-nowicki May 28, 2024
624573a
did not mean to cache user pick as that could change
chris-nowicki May 28, 2024
ff8b409
modified cache for getGameData to cache the getCurrentGame in
chris-nowicki May 28, 2024
5ce141f
removed unused import
chris-nowicki May 28, 2024
2f57eed
Merge branch 'develop' of github.com:LetsGetTechnical/gridiron-surviv…
chris-nowicki May 29, 2024
42e957d
Merge branch 'develop' of github.com:LetsGetTechnical/gridiron-surviv…
chris-nowicki May 29, 2024
3f59512
Merge branch 'develop' of github.com:LetsGetTechnical/gridiron-surviv…
chris-nowicki May 29, 2024
a4b7a50
Merge branch 'develop' of github.com:LetsGetTechnical/gridiron-surviv…
chris-nowicki May 29, 2024
e5d18b6
Merge branch 'develop' into mai/fix-cookie-weeklyapi
chris-nowicki May 30, 2024
9716ce6
fix: merge conflicts with develop
chris-nowicki May 30, 2024
cd5e316
updated data store with current week
chris-nowicki May 30, 2024
ac421f7
added back in isSignedIn to context
chris-nowicki May 30, 2024
839cdd0
fix: removed react cache as it is no longer valid way to cache in Nex…
chris-nowicki May 30, 2024
3bb1dbc
fix: updated functions in utils to take objects for easier debugging
chris-nowicki May 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions api/IapiFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@ export interface IUser {
id: string;
email: string;
}
export interface IUserWeeklyPick {
userId: string;
weekNumber: string;
}
export interface IWeeklyPicks {
gameId: string;
gameWeekId: string;
userResults: string;
userResults: IUserPicksData | null;
}

export interface INFLTeam {
teamId: string;
teamName: string;
teamLogo: string;
}
export interface IUserPicksData {
[key: string]: {
team: string;
correct: boolean;
};
}
export interface IGameGroup {
currentGameId: string;
participants: string[];
survivors: string[];
}
export interface IGameWeek {
id: string;
week: number;
}
140 changes: 91 additions & 49 deletions api/apiFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import { Models } from 'appwrite/types/models';
import { account, databases, ID, appwriteConfig } from './config';
import { IAccountData, IUserWeeklyPick, IWeeklyPicks } from './IapiFunctions';
import {
IAccountData,
IGameGroup,
IGameWeek,
IUser,
IWeeklyPicks,
} from './IapiFunctions';
import { Collection } from './EapiFunctions';
import { Query } from 'appwrite';

/**
* Get the current session of the user
* Register a new account
*
* @return {Models.User<Models.Preferences> | Error} - The user object or an error
*/
export async function registerAccount({
email,
password,
}: IAccountData): Promise<Models.User<Models.Preferences>> {
try {
return await account.create(ID.unique(), email, password);
} catch (error) {
console.error(error);
throw new Error('Error registering user');
}
}

/**
* Login to an existing account
*
* @param email - The email of the user
* @param password - The password of the user
Expand All @@ -23,7 +47,7 @@ export async function loginAccount({
}

/**
* Logout the current session of the user
* Logout the current user
*
* @return {Object | Error} - The session object or an error
*/
Expand All @@ -37,87 +61,103 @@ export async function logoutAccount(): Promise<{}> {
}

/**
* Get user's weekly pick
* Get all NFL teams
*
* @return {String} - The session object or an error
* @return {Models.DocumentList<Models.Document> | Error} - The list of NFL teams
*/
export async function getUserWeeklyPick({
userId,
weekNumber,
}: IUserWeeklyPick): Promise<string> {
// TODO: Use actual userId and weekNumber
export const getNFLTeams = async (): Promise<Models.Document[]> => {
try {
const response = await databases.listDocuments(
appwriteConfig.databaseId,
Collection.GAME_RESULTS,
Collection.NFL_TEAMS,
);
return response.documents[0].userResults;

return response.documents;
} catch (error) {
console.error(error);
throw new Error('Error getting user weekly pick');
throw new Error('Error getting NFL teams');
}
}
};

/**
* Get all weekly picks
* Get game the user is a part of
*
*
* @return {Models.DocumentList<Models.Document>} - The session object or an error
*/
export async function getAllWeeklyPicks(): Promise<
Models.DocumentList<Models.Document>
> {
export const getCurrentGame = async (
userId: IUser['id'],
): Promise<Models.Document> => {
try {
const response = await databases.listDocuments(
appwriteConfig.databaseId,
Collection.GAME_RESULTS,
'6626a937b6302f6a4d28',
[Query.contains('participants', userId)],
);

const data = JSON.parse(response.documents[0].userResults);
return data;
return response.documents[0];
} catch (error) {
console.error(error);
throw new Error('Error getting all weekly picks');
console.error('Error getting all game groups:', error);
throw new Error('Error getting all game groups');
}
}
};

/**
* Get all NFL teams
* Get the current week's ID & number
*
*
* @return {Models.DocumentList<Models.Document>} - The session object or an error
*/
export async function getNFLTeams(): Promise<
Models.DocumentList<Models.Document>
> {
export const getCurrentWeek = async (): Promise<IGameWeek> => {
try {
return await databases.listDocuments(
const response = await databases.getDocument(
appwriteConfig.databaseId,
Collection.NFL_TEAMS,
'current_week',
'664cfd88003c6cf2ff75',
);

return {
id: response.gameWeek.$id,
week: response.gameWeek.week,
};
} catch (error) {
console.error(error);
throw new Error('Error getting NFL teams');
console.error('Error getting current week:', error);
throw new Error('Error getting current week');
}
}
};

/**
* Register a new account
* Get all weekly picks
*
*
* @return {Models.User<Models.Preferences> | Error} - The user object or an error
*/
export async function registerAccount({
email,
password,
}: IAccountData): Promise<Models.User<Models.Preferences>> {
export async function getAllWeeklyPicks({
gameId,
weekId,
}: {
gameId: IGameGroup['currentGameId'];
weekId: IGameWeek['id'];
}): Promise<IWeeklyPicks['userResults'] | null> {
try {
return await account.create(ID.unique(), email, password);
const response = await databases.listDocuments(
appwriteConfig.databaseId,
Collection.GAME_RESULTS,
[Query.equal('gameId', gameId), Query.equal('gameWeekId', weekId)],
);

// check if any users have selected their pick
if (response.documents[0].userResults === '') {
return null;
}

const data = JSON.parse(response.documents[0].userResults);
return data;
} catch (error) {
console.error(error);
throw new Error('Error registering user');
throw new Error('Error getting all weekly picks');
}
}

/**
* Get the current user
* Update the weekly picks with the users team pick
*
* @return {Models.User<Models.Preferences> | Error} - The user object or an error
*/
Expand All @@ -126,14 +166,16 @@ export async function createWeeklyPicks({
gameWeekId,
userResults,
}: IWeeklyPicks): Promise<Models.Document> {
const data = { gameId, gameWeekId, userResults };

try {
return await databases.updateDocument(
appwriteConfig.databaseId,
Collection.GAME_RESULTS,
'663130a100297f77c3c8',
data,
Collection.GAME_RESULTS, //collectionID
'663130a100297f77c3c8', //documentID
{
gameId,
gameWeekId,
userResults: JSON.stringify(userResults),
},
);
} catch (error) {
console.error(error);
Expand Down
Loading
Loading