Restructure project (#124)
This commit is contained in:
23
front/src/services/api/people/__tests__/select.test.ts
Normal file
23
front/src/services/api/people/__tests__/select.test.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { reduceSortsToOrderBy } from '../../../../components/table/table-header/helpers';
|
||||
import { PeopleSelectedSortType } from '../select';
|
||||
|
||||
describe('reduceSortsToOrderBy', () => {
|
||||
it('should return an array of objects with the id as key and the order as value', () => {
|
||||
const sorts = [
|
||||
{
|
||||
key: 'firstname',
|
||||
label: 'firstname',
|
||||
order: 'asc',
|
||||
_type: 'default_sort',
|
||||
},
|
||||
{
|
||||
key: 'lastname',
|
||||
label: 'lastname',
|
||||
order: 'desc',
|
||||
_type: 'default_sort',
|
||||
},
|
||||
] satisfies PeopleSelectedSortType[];
|
||||
const result = reduceSortsToOrderBy(sorts);
|
||||
expect(result).toEqual([{ firstname: 'asc' }, { lastname: 'desc' }]);
|
||||
});
|
||||
});
|
||||
50
front/src/services/api/people/__tests__/update.test.ts
Normal file
50
front/src/services/api/people/__tests__/update.test.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import {
|
||||
GraphqlMutationPerson,
|
||||
GraphqlQueryPerson,
|
||||
} from '../../../../interfaces/entities/person.interface';
|
||||
import { updatePerson } from '../update';
|
||||
|
||||
jest.mock('../../../../apollo', () => {
|
||||
const personInterface = jest.requireActual(
|
||||
'../../../../interfaces/entities/person.interface',
|
||||
);
|
||||
return {
|
||||
apiClient: {
|
||||
mutate: (arg: {
|
||||
mutation: unknown;
|
||||
variables: GraphqlMutationPerson;
|
||||
}) => {
|
||||
const gqlPerson = arg.variables as unknown as GraphqlQueryPerson;
|
||||
return { data: personInterface.mapToPerson(gqlPerson) };
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
it('updates a person', async () => {
|
||||
const result = await updatePerson({
|
||||
firstname: 'John',
|
||||
lastname: 'Doe',
|
||||
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6c',
|
||||
email: 'john@example.com',
|
||||
company: {
|
||||
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
|
||||
name: 'ACME',
|
||||
domainName: 'example.com',
|
||||
__typename: 'companies',
|
||||
},
|
||||
phone: '+1 (555) 123-4567',
|
||||
pipes: [
|
||||
{
|
||||
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6d',
|
||||
name: 'Customer',
|
||||
icon: '!',
|
||||
},
|
||||
],
|
||||
creationDate: new Date(),
|
||||
city: 'San Francisco',
|
||||
__typename: 'people',
|
||||
});
|
||||
expect(result.data).toBeDefined();
|
||||
result.data && expect(result.data.email).toBe('john@example.com');
|
||||
});
|
||||
2
front/src/services/api/people/index.ts
Normal file
2
front/src/services/api/people/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './select';
|
||||
export * from './update';
|
||||
48
front/src/services/api/people/select.ts
Normal file
48
front/src/services/api/people/select.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { QueryResult, gql, useQuery } from '@apollo/client';
|
||||
import { GraphqlQueryPerson } from '../../../interfaces/entities/person.interface';
|
||||
import {
|
||||
Order_By,
|
||||
People_Bool_Exp,
|
||||
People_Order_By,
|
||||
} from '../../../generated/graphql';
|
||||
import { SelectedSortType } from '../../../interfaces/sorts/interface';
|
||||
|
||||
export type PeopleSelectedSortType = SelectedSortType<People_Order_By>;
|
||||
|
||||
export const GET_PEOPLE = gql`
|
||||
query GetPeople(
|
||||
$orderBy: [people_order_by!]
|
||||
$where: people_bool_exp
|
||||
$limit: Int
|
||||
) {
|
||||
people(order_by: $orderBy, where: $where, limit: $limit) {
|
||||
id
|
||||
phone
|
||||
email
|
||||
city
|
||||
firstname
|
||||
lastname
|
||||
created_at
|
||||
company {
|
||||
id
|
||||
name
|
||||
domain_name
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export function usePeopleQuery(
|
||||
orderBy: People_Order_By[],
|
||||
where: People_Bool_Exp,
|
||||
): QueryResult<{ people: GraphqlQueryPerson[] }> {
|
||||
return useQuery<{ people: GraphqlQueryPerson[] }>(GET_PEOPLE, {
|
||||
variables: { orderBy, where },
|
||||
});
|
||||
}
|
||||
|
||||
export const defaultOrderBy: People_Order_By[] = [
|
||||
{
|
||||
created_at: Order_By.Desc,
|
||||
},
|
||||
];
|
||||
143
front/src/services/api/people/update.ts
Normal file
143
front/src/services/api/people/update.ts
Normal file
@ -0,0 +1,143 @@
|
||||
import { FetchResult, gql } from '@apollo/client';
|
||||
import {
|
||||
Person,
|
||||
mapToGqlPerson,
|
||||
} from '../../../interfaces/entities/person.interface';
|
||||
import { apiClient } from '../../../apollo';
|
||||
|
||||
export const UPDATE_PERSON = gql`
|
||||
mutation UpdatePeople(
|
||||
$id: uuid
|
||||
$firstname: String
|
||||
$lastname: String
|
||||
$phone: String
|
||||
$city: String
|
||||
$company_id: uuid
|
||||
$email: String
|
||||
$created_at: timestamptz
|
||||
) {
|
||||
update_people(
|
||||
where: { id: { _eq: $id } }
|
||||
_set: {
|
||||
city: $city
|
||||
company_id: $company_id
|
||||
email: $email
|
||||
firstname: $firstname
|
||||
id: $id
|
||||
lastname: $lastname
|
||||
phone: $phone
|
||||
created_at: $created_at
|
||||
}
|
||||
) {
|
||||
returning {
|
||||
city
|
||||
company {
|
||||
domain_name
|
||||
name
|
||||
id
|
||||
}
|
||||
email
|
||||
firstname
|
||||
id
|
||||
lastname
|
||||
phone
|
||||
created_at
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const INSERT_PERSON = gql`
|
||||
mutation InsertPerson(
|
||||
$id: uuid
|
||||
$firstname: String
|
||||
$lastname: String
|
||||
$phone: String
|
||||
$city: String
|
||||
$company_id: uuid
|
||||
$email: String
|
||||
$created_at: timestamptz
|
||||
) {
|
||||
insert_people(
|
||||
objects: {
|
||||
id: $id
|
||||
firstname: $firstname
|
||||
lastname: $lastname
|
||||
phone: $phone
|
||||
city: $city
|
||||
company_id: $company_id
|
||||
email: $email
|
||||
created_at: $created_at
|
||||
}
|
||||
) {
|
||||
affected_rows
|
||||
returning {
|
||||
city
|
||||
company {
|
||||
domain_name
|
||||
name
|
||||
id
|
||||
}
|
||||
email
|
||||
firstname
|
||||
id
|
||||
lastname
|
||||
phone
|
||||
created_at
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const DELETE_PEOPLE = gql`
|
||||
mutation DeletePeople($ids: [uuid]) {
|
||||
delete_people(where: { id: { _in: $ids } }) {
|
||||
returning {
|
||||
city
|
||||
company {
|
||||
domain_name
|
||||
name
|
||||
id
|
||||
}
|
||||
email
|
||||
firstname
|
||||
id
|
||||
lastname
|
||||
phone
|
||||
created_at
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export async function updatePerson(
|
||||
person: Person,
|
||||
): Promise<FetchResult<Person>> {
|
||||
const result = await apiClient.mutate({
|
||||
mutation: UPDATE_PERSON,
|
||||
variables: mapToGqlPerson(person),
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function insertPerson(
|
||||
person: Person,
|
||||
): Promise<FetchResult<Person>> {
|
||||
const result = await apiClient.mutate({
|
||||
mutation: INSERT_PERSON,
|
||||
variables: mapToGqlPerson(person),
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function deletePeople(
|
||||
peopleIds: string[],
|
||||
): Promise<FetchResult<Person>> {
|
||||
const result = await apiClient.mutate({
|
||||
mutation: DELETE_PEOPLE,
|
||||
variables: { ids: peopleIds },
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user