Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATS Client Push #184

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion app/src/controllers/ats.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { atsService } from '../services';

import { getCurrentUsername } from '../utils/utils';

import type { NextFunction, Request, Response } from 'express';
import type { ATSUserSearchParameters } from '../types';
import type { ATSClientResource, ATSUserSearchParameters } from '../types';

const controller = {
searchATSUsers: async (
Expand All @@ -12,6 +14,19 @@ const controller = {
try {
const response = await atsService.searchATSUsers(req.query);

res.status(response.status).json(response.data);
} catch (e: unknown) {
next(e);
}
},

createATSClient: async (req: Request<never, never, ATSClientResource, never>, res: Response, next: NextFunction) => {
try {
const identityProvider = req.currentContext?.tokenPayload?.identity_provider.toUpperCase();
const atsClient = req.body;
// Set the createdBy field to current user with \\ as the separator for the domain and username to match ATS DB
atsClient.createdBy = `${identityProvider}\\${getCurrentUsername(req.currentContext)}`;
const response = await atsService.createATSClient(atsClient);
res.status(response.status).json(response.data);
} catch (e: unknown) {
next(e);
Expand Down
13 changes: 12 additions & 1 deletion app/src/routes/v1/ats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { hasAuthorization } from '../../middleware/authorization';
import { requireSomeAuth } from '../../middleware/requireSomeAuth';
import { requireSomeGroup } from '../../middleware/requireSomeGroup';
import { Action, Resource } from '../../utils/enums/application';
import { atsValidator } from '../../validators';

import type { NextFunction, Request, Response } from 'express';
import type { ATSUserSearchParameters } from '../../types';
import type { ATSClientResource, ATSUserSearchParameters } from '../../types';

const router = express.Router();
router.use(requireSomeAuth);
Expand All @@ -21,4 +22,14 @@ router.get(
}
);

/** Creates a client in ATS */
router.post(
'/client',
hasAuthorization(Resource.ATS, Action.CREATE),
atsValidator.createATSClient,
(req: Request<never, never, ATSClientResource, never>, res: Response, next: NextFunction): void => {
atsController.createATSClient(req, res, next);
}
);

export default router;
30 changes: 26 additions & 4 deletions app/src/services/ats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import axios from 'axios';
import config from 'config';

import type { AxiosInstance } from 'axios';
import type { ATSUserSearchParameters } from '../types';
import type { ATSClientResource, ATSUserSearchParameters } from '../types';

/**
* @function getToken
* Gets Auth token using ATS client credentials
* @returns
*/

async function getToken() {
const response = await axios({
method: 'GET',
Expand All @@ -33,7 +32,6 @@ async function getToken() {
* @param {AxiosRequestConfig} options Axios request config options
* @returns {AxiosInstance} An axios instance
*/

function atsAxios(): AxiosInstance {
// Create axios instance
const atsAxios = axios.create({
Expand All @@ -57,7 +55,6 @@ const service = {
* @param {ATSUserSearchParameters} data The search parameters
* @returns {Promise<data | null>} The result of calling the search api
*/

searchATSUsers: async (params?: ATSUserSearchParameters) => {
try {
const { data, status } = await atsAxios().get('/clients', { params: params });
Expand All @@ -75,6 +72,31 @@ const service = {
};
}
}
},

/**
* @function createATSClient
* Creates a client in ATS
* @param {ATSClientResource} data The client data
* @returns {Promise<data | null>} The result of calling the post api
*/
createATSClient: async (atsClient: ATSClientResource) => {
try {
const { data, status } = await atsAxios().post('/clients', atsClient);
return { data: data, status };
} catch (e: unknown) {
if (axios.isAxiosError(e)) {
return {
data: e.response?.data.message,
status: e.response ? e.response.status : 500
};
} else {
return {
data: 'Error',
status: 500
};
}
}
}
};

Expand Down
21 changes: 21 additions & 0 deletions app/src/types/ATSClientResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type AddressResource = {
'@type': string;
addressLine1: string;
addressLine2: string | null;
city: string;
provinceCode: string;
countryCode: string;
postalCode: string | null;
primaryPhone: string;
email: string;
};

export type ATSClientResource = {
'@type': string;
address: AddressResource;
firstName: string;
surName: string;
regionName: string;
optOutOfBCStatSurveyInd: string;
createdBy: string;
};
1 change: 1 addition & 0 deletions app/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type { Activity } from './Activity';
export type { AccessRequest } from './AccessRequest';
export type { ATSClientResource } from './ATSClientResource';
export type { ATSUserSearchParameters } from './ATSUserSearchParameters';
export type { BceidSearchParameters } from './BceidSearchParameters';
export type { BringForward } from './BringForward';
Expand Down
33 changes: 33 additions & 0 deletions app/src/validators/ats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Joi from 'joi';

import { validate } from '../middleware/validation';

import { BasicResponse } from '../utils/enums/application';

const addressBody = {
'@type': Joi.string().valid('AddressResource'),
addressLine1: Joi.string().max(255).allow(null),
city: Joi.string().max(255).allow(null),
provinceCode: Joi.string().max(255).allow(null),
primaryPhone: Joi.string().max(255).allow(null),
email: Joi.string().max(255).allow(null)
};

const clientBody = {
'@type': Joi.string().valid('ClientResource'),
firstName: Joi.string().max(255).required(),
surName: Joi.string().max(255).required(),
regionName: Joi.string().max(255).required(),
optOutOfBCStatSurveyInd: Joi.string().valid(BasicResponse.NO.toUpperCase()),
address: Joi.object(addressBody).allow(null)
};

const schema = {
createATSClient: {
body: Joi.object(clientBody)
}
};

export default {
createATSClient: validate(schema.createATSClient)
};
1 change: 1 addition & 0 deletions app/src/validators/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as accessRequestValidator } from './accessRequest';
export { default as atsValidator } from './ats';
export { default as documentValidator } from './document';
export { default as enquiryValidator } from './enquiry';
export { default as noteValidator } from './note';
Expand Down
Loading
Loading