All files / pages/Admin AdminJobsPage.jsx

100% Statements 19/19
100% Branches 0/0
100% Functions 8/8
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                    10x         10x           10x 1x       10x         10x           10x 1x       10x         10x           10x 1x     10x                   10x                                             10x       20x                              
import React from "react";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import JobsTable from "main/components/Jobs/JobsTable";
import { useBackend } from "main/utils/useBackend";
import { Button } from "react-bootstrap";
import Accordion from "react-bootstrap/Accordion";
import SingleButtonJobForm from "main/components/Jobs/SingleButtonJobForm";
import { useBackendMutation } from "main/utils/useBackend";
 
export default function AdminJobsPage() {
  const objectToAxiosParamsUpdateAllJob = () => ({
    url: "/api/jobs/launch/updateAll",
    method: "POST",
  });
 
  const updateAllJobMutation = useBackendMutation(
    objectToAxiosParamsUpdateAllJob,
    {},
    ["/api/jobs/all"],
  );
 
  const submitUpdateAllJob = async () => {
    updateAllJobMutation.mutate();
  };
 
  // AuditAllCourses job
  const objectToAxiosParamsAuditAllCoursesJob = () => ({
    url: "/api/jobs/launch/auditAllCourses",
    method: "POST",
  });
 
  const auditAllCoursesJobMutation = useBackendMutation(
    objectToAxiosParamsAuditAllCoursesJob,
    {},
    ["/api/jobs/all"],
  );
 
  const submitAuditAllCoursesJob = async () => {
    auditAllCoursesJobMutation.mutate();
  };
 
  // purge job
  const objectToAxiosParamsPurgeJobLog = () => ({
    url: "/api/jobs/all",
    method: "DELETE",
  });
 
  const purgeJobLogMutation = useBackendMutation(
    objectToAxiosParamsPurgeJobLog,
    {},
    ["/api/jobs/all"],
  );
 
  const purgeJobLog = async () => {
    purgeJobLogMutation.mutate();
  };
 
  const { data: jobs } = useBackend(
    ["/api/jobs/all"],
    {
      //Stryker disable next-line StringLiteral: axios default is GET
      method: "GET",
      url: "/api/jobs/all",
    },
    [],
  );
 
  const jobLaunchers = [
    {
      name: "Update All Users",
      form: (
        <SingleButtonJobForm
          callback={submitUpdateAllJob}
          text={"Start"}
          testid={"updateAllJob"}
        />
      ),
    },
    {
      name: "Audit All Courses",
      form: (
        <SingleButtonJobForm
          callback={submitAuditAllCoursesJob}
          text={"Start"}
          testid={"auditAllCoursesJob"}
        />
      ),
    },
  ];
 
  return (
    <BasicLayout>
      <h2 className="p-3">Launch Jobs</h2>
      <Accordion>
        {jobLaunchers.map((jobLauncher, index) => (
          <Accordion.Item eventKey={index} key={index}>
            <Accordion.Header>{jobLauncher.name}</Accordion.Header>
            <Accordion.Body>{jobLauncher.form}</Accordion.Body>
          </Accordion.Item>
        ))}
      </Accordion>
      <h2 className="p-3">Job Status</h2>
      <JobsTable jobs={jobs} />
      <Button variant="danger" onClick={purgeJobLog} data-testid="purgeJobLog">
        Purge Job Log
      </Button>
    </BasicLayout>
  );
}