Skip to content

Commit

Permalink
Release v0.4.0-alpha (#501)
Browse files Browse the repository at this point in the history
Release 0.4.0-alpha
  • Loading branch information
shashilo authored Sep 5, 2024
2 parents 7139b94 + 650a3e1 commit 9a6053c
Show file tree
Hide file tree
Showing 29 changed files with 1,904 additions and 165 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
**/*.test.js
**/*.test.jsx
next-env.d.ts
storybook-static/
37 changes: 37 additions & 0 deletions api/apiFunctions.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,40 @@ export enum GameWeek {
export enum Document {
CURRENT_GAME_WEEK = '664cfd88003c6cf2ff75',
}

/**
* Enum representing NFL teams with their corresponding unique identifiers.
*/
export enum NFLTeams {
'49ers' = '49ers',
'bears' = 'Bears',
'bengals' = 'Bengals',
'bills' = 'Bills',
'broncos' = 'Broncos',
'browns' = 'Browns',
'buccaneers' = 'Buccaneers',
'cardinals' = 'Cardinals',
'chargers' = 'Chargers',
'chiefs' = 'Chiefs',
'commanders' = 'Commanders',
'cowboys' = 'Cowboys',
'dolphins' = 'Dolphins',
'eagles' = 'Eagles',
'falcons' = 'Falcons',
'giants' = 'Giants',
'jaguars' = 'Jaguars',
'jets' = 'Jets',
'lions' = 'Lions',
'packers' = 'Packers',
'panthers' = 'Panthers',
'patriots' = 'Patriots',
'raiders' = 'Raiders',
'rams' = 'Rams',
'ravens' = 'Ravens',
'saints' = 'Saints',
'seahawks' = 'Seahawks',
'steelers' = 'Steelers',
'texans' = 'Texans',
'titans' = 'Titans',
'vikings' = 'Vikings',
}
9 changes: 6 additions & 3 deletions api/apiFunctions.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ export interface IAccountData {
password: string;
}
export interface IUser {
documentId: string;
id: string;
email: string;
leagues: string[];
}
export interface IUserPick {
[userId: string]: {
[entryId: IEntry['$id']]: {
teamName: string;
correct: boolean;
[leagueId: string]: {
[entryId: IEntry['$id']]: {
teamName: string;
correct: boolean;
};
};
};
}
Expand Down
113 changes: 112 additions & 1 deletion api/apiFunctions.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { loginAccount, logoutAccount, registerAccount } from './apiFunctions';
import { log } from 'console';
import {
addUserToLeague,
getAllLeagues,
getUserDocumentId,
loginAccount,
logoutAccount,
registerAccount,
} from './apiFunctions';
import { account } from './config';
const apiFunctions = require('./apiFunctions');

Expand All @@ -10,6 +18,9 @@ jest.mock('./apiFunctions', () => {
getUserWeeklyPick: jest.fn(),
getAllWeeklyPicks: jest.fn(),
getCurrentUserEntries: jest.fn(),
getAllLeagues: jest.fn(),
getUserDocumentId: jest.fn(),
addUserToLeague: jest.fn(),
};
});

Expand Down Expand Up @@ -204,3 +215,103 @@ describe('getCurrentUserEntries()', () => {
}
});
});

xdescribe('get all leagues', () => {
it('should return all leagues upon successful call', async () => {
const mockAllLeagues = [
{
leagueId: '1',
leagueName: 'League One',
logo: '',
participants: ['user1', 'user2'],
survivors: ['user1'],
},
{
leagueId: '2',
leagueName: 'League Two',
logo: '',
participants: ['user3', 'user4'],
survivors: ['user4'],
},
];

apiFunctions.getAllLeagues.mockResolvedValue(mockAllLeagues);

const result = await apiFunctions.getAllLeagues();

expect(result).toEqual([
{
leagueId: '1',
leagueName: 'League One',
logo: '',
participants: ['user1', 'user2'],
survivors: ['user1'],
},
{
leagueId: '2',
leagueName: 'League Two',
logo: '',
participants: ['user3', 'user4'],
survivors: ['user4'],
},
]);
});
it('should return error upon unsuccessful call', async () => {
apiFunctions.getAllLeagues.mockRejectedValue(
new Error('Error getting all leagues'),
);

await expect(apiFunctions.getAllLeagues()).rejects.toThrow(
'Error getting all leagues',
);
});
});

describe('get users id', () => {
it('should return user document ID', async () => {
const userId = '123';
const response = '456';
apiFunctions.getUserDocumentId.mockResolvedValue(response);
const result = await apiFunctions.getUserDocumentId(userId);
expect(result).toEqual(response);
});
it('should return error upon unsuccessful call', async () => {
apiFunctions.getUserDocumentId.mockRejectedValue(
new Error('Error getting user document ID'),
);
await expect(apiFunctions.getUserDocumentId('123')).rejects.toThrow(
'Error getting user document ID',
);
});
});

describe('add user to league', () => {
const response = {
userDocumentId: 'user123',
selectedLeague: 'league123',
selectedLeagues: ['league123'],
participants: ['user123', 'user456'],
survivors: ['user123'],
};
it('should add user to league', async () => {
apiFunctions.addUserToLeague.mockResolvedValue(response);
const result = await apiFunctions.addUserToLeague({ response });
expect(result).toEqual(response);
});
it('should return error upon unsuccessful call', async () => {
apiFunctions.addUserToLeague.mockRejectedValue(
new Error('Error adding user to league'),
);
await expect(apiFunctions.addUserToLeague('123', '456')).rejects.toThrow(
'Error adding user to league',
);
});
it('should return error upon unsuccessful call', async () => {
apiFunctions.addUserToLeague.mockRejectedValue(
new Error('Error adding user to league'),
);
await expect(apiFunctions.addUserToLeague({ response })).rejects.toThrow(
'Error adding user to league',
);
});
});
108 changes: 103 additions & 5 deletions api/apiFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export async function getCurrentUser(userId: IUser['id']): Promise<IUser> {
[Query.equal('userId', userId)],
);
return {
documentId: user.documents[0].$id,
id: user.documents[0].userId,
email: user.documents[0].email,
leagues: user.documents[0].leagues,
Expand Down Expand Up @@ -226,11 +227,6 @@ export async function getAllWeeklyPicks({
return null;
}

// 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) {
Expand Down Expand Up @@ -301,3 +297,105 @@ export async function createEntry({
throw new Error('Error creating entry');
}
}

/**
* Update an entry
* @param props - The entry data
* @param props.entryId - The entry ID
* @param props.selectedTeams - The selected teams
* @returns {Models.Document | Error} - The entry object or an error
*/
export async function updateEntry({
entryId,
selectedTeams,
}: {
entryId: string;
selectedTeams: INFLTeam['teamName'][];
}): Promise<Models.Document & IEntry> {
try {
return await databases.updateDocument(
appwriteConfig.databaseId,
Collection.ENTRIES,
entryId,
{
selectedTeams,
},
);
} catch (error) {
console.error(error);
throw new Error('Error updating entry');
}
}

/**
* Retrieves a list of all leagues.
* @returns {Models.Document[]} A list of all available leagues.
*/
export async function getAllLeagues(): Promise<ILeague[]> {
try {
const response = await databases.listDocuments(
appwriteConfig.databaseId,
Collection.LEAGUE,
);

// loop through leagues and return ILeague[] instead of Models.Document[]
const leagues = response.documents.map((league) => ({
leagueId: league.$id,
leagueName: league.leagueName,
logo: '',
participants: league.participants,
survivors: league.survivors,
}));

return leagues;
} catch (error) {
throw new Error('Error getting all leagues', { cause: error });
}
}

/**
* Adds a user to a league by updating the user's entry document.
* @param {string} userDocumentId - The ID of the user to add to the league.
* @param {string} selectedLeague - The ID of the league to add the user to.
* @param selectedLeagues - The user selected leagues
* @param participants - The user's participants
* @param survivors - The user's survivors
* @returns {Promise<void>} A promise that resolves when the user has been added to the league.
*/
export async function addUserToLeague({
userDocumentId,
selectedLeague,
selectedLeagues,
participants,
survivors,
}: {
userDocumentId: string;
selectedLeague: string;
selectedLeagues: string[];
participants: string[];
survivors: string[];
}): Promise<void> {
try {
await databases.updateDocument(
appwriteConfig.databaseId,
Collection.USERS,
userDocumentId,
{
leagues: selectedLeagues,
},
);

await databases.updateDocument(
appwriteConfig.databaseId,
Collection.LEAGUE,
selectedLeague,
{
participants: participants,
survivors: survivors,
},
);
} catch (error) {
throw new Error('Error getting user document ID', { cause: error });
}
}
4 changes: 2 additions & 2 deletions app/(main)/league/[leagueId]/entry/Entries.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export interface IEntry {
name: string;
user: IUser;
league: ILeague;
selectedTeams: INFLTeam[];
selectedTeams: INFLTeam['teamName'][];
eliminated: boolean;
}

export interface IEntryProps {
name: string;
user: IUser['id'];
league: ILeague['leagueId'];
selectedTeams?: INFLTeam[];
selectedTeams?: INFLTeam['teamName'][];
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import {
IWeeklyPicks,
IUser,
} from '@/api/apiFunctions.interface';

export interface IWeekData {
target: {
value: string;
};
}
import { NFLTeams } from '@/api/apiFunctions.enum';

export interface IWeekParams {
params: {
Expand All @@ -24,7 +19,7 @@ export interface IWeekParams {
}

export interface IWeeklyPickChange {
data: IWeekData;
teamSelect: NFLTeams;
entry: string;
league: string;
NFLTeams: INFLTeam[];
Expand Down
Loading

1 comment on commit 9a6053c

@vercel
Copy link

@vercel vercel bot commented on 9a6053c Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.