Skip to content

Commit bfef7fd

Browse files
authored
Merge pull request #3386 from Northeastern-Electric-Racing/#3376-JhonyelGalvis-HookUpGet/Put/Delete
created the hooks for getting sponsors, sponsor-tasks, deleting a spo…
2 parents a7149d6 + fd3fc37 commit bfef7fd

File tree

3 files changed

+143
-7
lines changed

3 files changed

+143
-7
lines changed

src/frontend/src/apis/finance.api.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
* This file is part of NER's FinishLine and licensed under GNU AGPLv3.
33
* See the LICENSE file in the repository root folder for details.
44
*/
5+
import { sponsorTaskTranformer, sponsorTransformer } from '../../../backend/src/transformers/finance.transformer';
56
import {
67
CreateReimbursementRequestPayload,
78
EditReimbursementRequestPayload,
89
EditVendorPayload,
910
AccountCodePayload,
1011
RefundPayload,
1112
MarkDeliveredRequestPayload,
13+
EditSponsorTaskPayload,
1214
SponsorPayload,
1315
SponsorTierPayload,
1416
SponsorTaskPayload
@@ -22,7 +24,7 @@ import {
2224
} from './transformers/reimbursement-requests.transformer';
2325
import { saveAs } from 'file-saver';
2426
import { PDFDocument, PDFImage } from 'pdf-lib';
25-
import { IndexCode, AccountCode, ReimbursementRequest, OtherProductReason } from 'shared';
27+
import { IndexCode, AccountCode, ReimbursementRequest, OtherProductReason, Sponsor, SponsorTask } from 'shared';
2628

2729
enum AllowedFileType {
2830
JPEG = 'image/jpeg',
@@ -424,7 +426,7 @@ export const createSponsorTask = async (sponsorId: string, sponsorTaskData: Spon
424426
*/
425427
export const getAllIndexCodes = () => {
426428
return axios.get<IndexCode[]>(apiUrls.getAllIndexCodes(), {
427-
transformResponse: (data) => JSON.parse(data) as IndexCode[]
429+
transformResponse: (data) => JSON.parse(data)
428430
});
429431
};
430432

@@ -435,6 +437,56 @@ export const getAllIndexCodes = () => {
435437
*/
436438
export const getAllOtherProductReason = () => {
437439
return axios.get<OtherProductReason[]>(apiUrls.getAllOtherProductReasons(), {
438-
transformResponse: (data) => JSON.parse(data) as OtherProductReason[]
440+
transformResponse: (data) => JSON.parse(data)
439441
});
440442
};
443+
444+
/**
445+
* API call to get the list of all sponsors
446+
*
447+
* @returns the list of all sponsors
448+
*/
449+
450+
export const getAllSponsors = () => {
451+
return axios.get<Sponsor[]>(apiUrls.getAllSponsors(), {
452+
transformResponse: (data) => JSON.parse(data).map(sponsorTransformer)
453+
});
454+
};
455+
456+
/**
457+
* API call to the sponsor tasks for a given sponsor
458+
*
459+
* @param sponsorId the id of the sponsor which tasks are retrieved
460+
*
461+
* @returns the list of tasks for a given sponsor
462+
*/
463+
464+
export const getSponsorTasks = (sponsorId: string) => {
465+
return axios.get<SponsorTask[]>(apiUrls.getSponsorTasks(sponsorId), {
466+
transformResponse: (data) => JSON.parse(data).map(sponsorTaskTranformer)
467+
});
468+
};
469+
470+
/**
471+
* API call to delete a given sponsor
472+
*
473+
* @param sponsorId the id of the sponsor to delete
474+
*
475+
* @returns the deleted sponsor
476+
*/
477+
478+
export const deleteSponsor = (sponsorId: string) => {
479+
return axios.delete(apiUrls.deleteSponsor(sponsorId));
480+
};
481+
482+
/**
483+
* API call to edit a sponsor task
484+
*
485+
* @param sponsorTaskData the edited data of the sponsor task
486+
* @param sponsorTaskId the id of the sponsor task to be edited
487+
*
488+
* @returns the edited sponosor task
489+
*/
490+
export const editSponsorTask = (sponsorTaskId: string, sponsorTaskData: EditSponsorTaskPayload) => {
491+
return axios.post(apiUrls.editSponsorTask(sponsorTaskId), sponsorTaskData);
492+
};

src/frontend/src/hooks/finance.hooks.ts

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ import {
3737
createSponsorTask,
3838
createSponsorTier,
3939
getAllIndexCodes,
40-
getAllOtherProductReason
40+
getAllOtherProductReason,
41+
getAllSponsors,
42+
getSponsorTasks,
43+
editSponsorTask,
44+
deleteSponsor
4145
} from '../apis/finance.api';
4246
import {
4347
IndexCode,
@@ -50,10 +54,10 @@ import {
5054
OtherReimbursementProductCreateArgs,
5155
WbsReimbursementProductCreateArgs,
5256
ReimbursementStatusType,
53-
SponsorTask,
57+
OtherProductReason,
5458
Sponsor,
55-
SponsorTier,
56-
OtherProductReason
59+
SponsorTask,
60+
SponsorTier
5761
} from 'shared';
5862
import { fullNamePipe } from '../utils/pipes';
5963

@@ -190,6 +194,13 @@ export interface IndexCodePayload {
190194
name: string;
191195
}
192196

197+
export interface EditSponsorTaskPayload {
198+
dueDate: Date;
199+
notes: string;
200+
notifyDate?: Date;
201+
asigneeId?: string;
202+
}
203+
193204
/**
194205
* Custom React Hook to upload a new picture.
195206
*/
@@ -715,3 +726,68 @@ export const useGetAllOtherProductReason = () => {
715726
return data;
716727
});
717728
};
729+
730+
/**
731+
* custom React Hook to get all the sponsors
732+
*
733+
* @returns the list of all of the sponsors
734+
*/
735+
export const useGetAllSponsors = () => {
736+
return useQuery<Sponsor[], Error>(['sponsor'], async () => {
737+
const { data } = await getAllSponsors();
738+
return data;
739+
});
740+
};
741+
742+
/**
743+
* custom react hook to get all of the sponsor tasks for a given sponsor
744+
*
745+
* @returns the list of all of the sponsor tasks for a given sponsor
746+
*/
747+
export const useGetSponsorTasks = (sponsorId: string) => {
748+
return useQuery<SponsorTask[], Error>(['sponsor-task'], async () => {
749+
const { data } = await getSponsorTasks(sponsorId);
750+
return data;
751+
});
752+
};
753+
754+
/**
755+
* Custom React Hook to edit a sponsor task
756+
*
757+
* @param sponsorTaskId the id of the sponsor task to be edited
758+
*
759+
* @returns the edited sponosor task
760+
*/
761+
export const useEditSponsorTask = (sponsorTaskId: string) => {
762+
const queryClient = useQueryClient();
763+
return useMutation<SponsorTask, Error, EditSponsorTaskPayload>(
764+
['sponsor-task', 'edit'],
765+
async (formData: EditSponsorTaskPayload) => {
766+
const { data } = await editSponsorTask(sponsorTaskId, formData);
767+
queryClient.invalidateQueries(['sponsor-task']);
768+
return data;
769+
}
770+
);
771+
};
772+
773+
/**
774+
* Custom React Hook to delete a sponsor
775+
*
776+
* @param sponsorId the id of the sponsor to be deleted
777+
* @returns the deleted sponsor
778+
*/
779+
export const useDeleteSponsor = (sponsorId: string) => {
780+
const queryClient = useQueryClient();
781+
return useMutation<Sponsor, Error>(
782+
['sponsor', 'delete'],
783+
async () => {
784+
const { data } = await deleteSponsor(sponsorId);
785+
return data;
786+
},
787+
{
788+
onSuccess: () => {
789+
queryClient.invalidateQueries(['sponsor']);
790+
}
791+
}
792+
);
793+
};

src/frontend/src/utils/urls.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ const financeCreateSponsorTier = () => `${financeRoutesEndpoints()}/sponsorTier/
148148
const financeCreateSponsorTask = (sponsorId: string) => `${financeRoutesEndpoints()}/sponsor/${sponsorId}/sponsorTasks`;
149149
const getAllIndexCodes = () => `${financeEndpoints()}/index-codes`;
150150
const getAllOtherProductReasons = () => `${financeEndpoints()}/other-reimbursement-product-reasons`;
151+
const getAllSponsors = () => `${financeRoutesEndpoints()}/sponsors`;
152+
const getSponsorTasks = (sponsorId: string) => `${financeRoutesEndpoints()}/sponsors/${sponsorId}/sponsorTasks`;
153+
const deleteSponsor = (sponsorId: string) => `${financeRoutesEndpoints()}/sponsors/${sponsorId}/delete`;
154+
const editSponsorTask = (sponsorTaskId: string) => `${financeRoutesEndpoints()}/sponsorTask/${sponsorTaskId}/edit`;
151155

152156
/**************** Bill of Material Endpoints **************************/
153157
const bomEndpoints = () => `${API_URL}/projects/bom`;
@@ -377,6 +381,10 @@ export const apiUrls = {
377381
financeCreateSponsorTask,
378382
getAllIndexCodes,
379383
getAllOtherProductReasons,
384+
getAllSponsors,
385+
getSponsorTasks,
386+
deleteSponsor,
387+
editSponsorTask,
380388

381389
bomEndpoints,
382390
bomGetMaterialsByWbsNum,

0 commit comments

Comments
 (0)