diff --git a/src/resolver.ts b/src/resolver.ts index 2eeba49..856f031 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -1,34 +1,31 @@ -// This file simulates a full-fledged GraphQL resolver in a simplified context for a take-home test. import {userData1, userData2} from "./data"; import {DataSource, User} from "./types"; export function getUsers(): User[] { - // Task: Combine userData1 and userData2 into a single array and return users normalized to match the schema. - // Hint: You may want to use the normalizeUser function on each user object before returning the final array. const rawUsers = [...userData1, ...userData2]; - - return []; // TODO: Replace this with the normalized list of users. + const normalizedUsers = rawUsers.map(normalizeUser); + return normalizedUsers; } export function getUserById(id: string | number): User | null { - // Task: Implement this function to return a user by their ID from the correct data source. - // Hint: Check the type of `id` to determine whether to search in userData1 or userData2. - // userData1 contains UUIDs (strings), while userData2 contains numerical IDs. + const user = typeof id === 'string' + ? userData1.find(userData => userData.id === id) + : userData2.find(userData => userData.id === id); + + if (user) { + return normalizeUser(user); + } - return null; // TODO: Implement the logic to find and return the appropriate user. + return null; } -// This function is intended to normalize user data from different formats into a uniform schema. function normalizeUser(user: any): User { - // Task: Adapt this function to convert an input user object into the format specified below. - // Hint: Map properties from both userData1 and userData2 to this uniform schema. - // Example mapping: `first` or `first_name` should map to `firstName`. return { - id: '', // TODO: Set this to the user's id. - firstName: '', // TODO: Set this to the user's first name, considering both 'first' and 'first_name'. - lastName: '', // TODO: Set this to the user's last name, considering both 'last' and 'last_name'. - emailAddress: '', // TODO: Set this to the user's email address, considering both 'email' and 'email_address'. - phoneNumber: '', // TODO: Set this to the user's phone number, considering both 'phone' and 'phone_number'. - source: DataSource.DATA_SOURCE_1 // Hint: Determine the source based on the ID type (string for DATA_SOURCE_1, number for DATA_SOURCE_2). + id: user.id, + firstName: user.first || user.first_name || '', + lastName: user.last || user.last_name || '', + emailAddress: user.email || user.email_address || '', + phoneNumber: user.phone || user.phone_number || '', + source: typeof user.id === 'string' ? DataSource.DATA_SOURCE_1 : DataSource.DATA_SOURCE_2 }; } diff --git a/src/types.ts b/src/types.ts index ddc452a..c94eefd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,6 +9,6 @@ export interface User { } export enum DataSource { - DATA_SOURCE_1, - DATA_SOURCE_2 + DATA_SOURCE_1 = "DATA_SOURCE_1", + DATA_SOURCE_2 = "DATA_SOURCE_2", } diff --git a/test/resolver.test.ts b/test/resolver.test.ts index aeb34e5..a2d2c9b 100644 --- a/test/resolver.test.ts +++ b/test/resolver.test.ts @@ -23,16 +23,16 @@ describe('Resolver Tests', () => { const uuid = 'c3d504e3-4f82-4e28-8db9-b3157647c8c1'; const user = getUserById(uuid); expect(user).not.toBeNull(); - expect(user.id).toEqual(uuid); - expect(user.source).toEqual(DataSource.DATA_SOURCE_1); + expect(user?.id).toEqual(uuid); + expect(user?.source).toEqual(DataSource.DATA_SOURCE_1); }); it('should return a user by numeric ID from DataSource.DATA_SOURCE_2', () => { const numericId = 1; const user = getUserById(numericId); expect(user).not.toBeNull(); - expect(user.id).toEqual(numericId); - expect(user.source).toEqual(DataSource.DATA_SOURCE_2); + expect(user?.id).toEqual(numericId); + expect(user?.source).toEqual(DataSource.DATA_SOURCE_2); }); // Test to verify that the correct data source is used based on the ID type @@ -42,10 +42,10 @@ describe('Resolver Tests', () => { const userFromUuid = getUserById(uuid); const userFromNumericId = getUserById(numericId); - expect(userFromUuid.source).toEqual(DataSource.DATA_SOURCE_1); - expect(typeof userFromUuid.id).toBe('string'); - expect(userFromNumericId.source).toEqual(DataSource.DATA_SOURCE_2); - expect(typeof userFromNumericId.id).toBe('number'); + expect(userFromUuid?.source).toEqual(DataSource.DATA_SOURCE_1); + expect(typeof userFromUuid?.id).toBe('string'); + expect(userFromNumericId?.source).toEqual(DataSource.DATA_SOURCE_2); + expect(typeof userFromNumericId?.id).toBe('number'); }); }); });