113 lines
3.4 KiB
JavaScript
113 lines
3.4 KiB
JavaScript
import EmployeeActionTypes from "./employee.types";
|
|
import {
|
|
employeeSignInStart,
|
|
employeeSignInSuccess,
|
|
employeeSignInFailure,
|
|
employeeGetRatesStart,
|
|
employeeGetRatesSuccess,
|
|
employeeGetRatesFailure
|
|
|
|
} from "./employee.actions";
|
|
import { all, call, put, select, takeLatest } from "redux-saga/effects";
|
|
import { logImEXEvent } from "../../firebase/firebase.analytics";
|
|
import { selectBodyshop } from "../user/user.selectors";
|
|
import axios from "axios";
|
|
import { client } from "../../graphql/client";
|
|
import { QUERY_EMPLOYEE_BY_ID } from "../../graphql/employees.queries";
|
|
import { selectCurrentEmployee } from "./employee.selectors";
|
|
|
|
export function* onSignInEmployeeStart() {
|
|
yield takeLatest(
|
|
EmployeeActionTypes.EMPLOYEE_SIGN_IN_START,
|
|
signInWithEmployeeId
|
|
);
|
|
}
|
|
export function* signInWithEmployeeId({ payload: { employeeId, pin } }) {
|
|
try {
|
|
logImEXEvent("redux_sign_in_employee_attempt");
|
|
//console.loging
|
|
// console.log("Saga", employeeId, pin, pin);
|
|
const bodyshop = yield select(selectBodyshop);
|
|
const response = yield call(axios.post, "/tech/login", {
|
|
shopid: bodyshop.id,
|
|
employeeid: employeeId,
|
|
pin: pin,
|
|
});
|
|
const { valid, technician, error } = response.data;
|
|
|
|
if (valid) {
|
|
yield put(employeeSignInSuccess(technician));
|
|
} else {
|
|
yield put(employeeSignInFailure(error));
|
|
}
|
|
} catch (error) {
|
|
yield put(employeeSignInFailure(error));
|
|
}
|
|
}
|
|
|
|
/*
|
|
//Waits for EMPLOYEE_SIGN_IN_SUCCESS when logging in to then call QUERY_EMPLOYEE_BY_ID with the id in payload to override technician with more data like rates and flat_rate
|
|
*/
|
|
export function* onEmployeeSignInSuccessSaga() {
|
|
yield takeLatest(
|
|
EmployeeActionTypes.EMPLOYEE_SIGN_IN_SUCCESS,
|
|
updateEmployeeWithEmployeeId
|
|
);
|
|
}
|
|
export function* updateEmployeeWithEmployeeId({ payload }) {
|
|
try {
|
|
const employeeId = payload.id;
|
|
// logImEXEvent("redux_update_employee_with_employee_id_attempt", employeeId);
|
|
const result = yield client.query({ query: QUERY_EMPLOYEE_BY_ID,
|
|
variables: {
|
|
id: employeeId,
|
|
}
|
|
});
|
|
const { employees_by_pk } = result.data;
|
|
if (employees_by_pk) {
|
|
yield put(employeeGetRatesSuccess(employees_by_pk));
|
|
} else {
|
|
yield put(employeeGetRatesFailure(result.error));
|
|
}
|
|
} catch (error) {
|
|
yield put(employeeGetRatesFailure(error));
|
|
console.log("Error while getting employee rates.", error);
|
|
//Sentry.Native.captureException(error);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function* onEmployeeGetRatesStart() {
|
|
yield takeLatest(
|
|
EmployeeActionTypes.EMPLOYEE_GET_RATES_START,
|
|
getRatesWithEmployeeId
|
|
);
|
|
}
|
|
export function* getRatesWithEmployeeId({ payload: employeeId }) {
|
|
try {
|
|
|
|
logImEXEvent("redux_employee_get_rates_attempt", employeeId);
|
|
const result = yield client.query({ query: QUERY_EMPLOYEE_BY_ID,
|
|
variables: {
|
|
id: employeeId,
|
|
}
|
|
});
|
|
const { employees_by_pk } = result.data;
|
|
|
|
if (employees_by_pk) {
|
|
yield put(employeeGetRatesSuccess(employees_by_pk));
|
|
} else {
|
|
yield put(employeeGetRatesFailure(error));
|
|
}
|
|
} catch (error) {
|
|
yield put(employeeGetRatesFailure(error));
|
|
console.log("Error while getting employee rates.", error);
|
|
//Sentry.Native.captureException(error);
|
|
}
|
|
}
|
|
|
|
export function* employeeSagas() {
|
|
yield all([call(onSignInEmployeeStart), call(onEmployeeSignInSuccessSaga), call(onEmployeeGetRatesStart)]);
|
|
}
|