Skip to content

Commit

Permalink
[DCJ-260][risk=no] Converted User.js -> User.ts (#2557)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkobori authored Jul 1, 2024
1 parent c13c894 commit 2159399
Show file tree
Hide file tree
Showing 6 changed files with 429 additions and 124 deletions.
124 changes: 0 additions & 124 deletions src/libs/ajax/User.js

This file was deleted.

168 changes: 168 additions & 0 deletions src/libs/ajax/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import * as fp from 'lodash/fp';
import { cloneDeep, flow, unset } from 'lodash/fp';
import { Config } from '../config';
import axios from 'axios';
import { getApiUrl, fetchOk, fetchAny } from '../ajax';
import { CreateDuosUserResponse, DuosUserResponse, UpdateDuosUserResponse } from 'src/types/responseTypes';
import { CreateDuosUserRequest, UpdateDuosUserRequestV1, UpdateDuosUserRequestV2 } from 'src/types/requestTypes';
import { AcknowledgementMap, ApprovedDataset, Dataset, DuosUser, SimplifiedDuosUser } from 'src/types/model';

export const User = {
getMe: async (): Promise<DuosUserResponse> => {
const url = `${await getApiUrl()}/api/user/me`;
const res = await axios.get(url, Config.authOpts());
return res.data;
},

getById: async (id: number): Promise<DuosUserResponse> => {
const url = `${await getApiUrl()}/api/user/${id}`;
const res = await axios.get(url, Config.authOpts());
return res.data;
},

list: async (roleName: 'Admin' | 'SigningOfficial'): Promise<DuosUserResponse[]> => {
const url = `${await getApiUrl()}/api/user/role/${roleName}`;
const res = await fetchOk(url, Config.authOpts());
return res.json();
},

create: async (request: CreateDuosUserRequest): Promise<CreateDuosUserResponse> => {
const url = `${await getApiUrl()}/api/dacuser`;
try {
const res = await fetchOk(
url,
fp.mergeAll([
Config.authOpts(),
Config.jsonBody(request),
{ method: 'POST' },
])
);
if (res.ok) {
return res.json();
}
} catch (err) {
return false;
}
},

updateSelf: async (payload: UpdateDuosUserRequestV1): Promise<UpdateDuosUserResponse> => {
const url = `${await getApiUrl()}/api/user`;
// We should not be updating the user's create date, associated institution, or library cards
try {
const res = await fetchOk(
url,
fp.mergeAll([
Config.authOpts(),
Config.jsonBody(payload),
{ method: 'PUT' },
])
);
if (res.ok) {
return res.json();
}
} catch (err) {
return false;
}
},

update: async (user: UpdateDuosUserRequestV2, userId: number)/*: Promise<UpdateDuosUserResponse>*/ => {
const url = `${await getApiUrl()}/api/user/${userId}`;
// We should not be updating the user's create date, associated institution, or library cards
// This below code does not seem to work at all and
// does not seem appropriate for this request anyway.
// The UpdateDuosUserRequestV2 is not the same shape as a DuosUser
// like this flow suggests.
const filteredUser = flow(
cloneDeep,
unset('updatedUser.createDate'),
unset('updatedUser.institution'),
unset('updatedUser.libraryCards')
)(user);
try {
const res = await fetchOk(
url,
fp.mergeAll([
Config.authOpts(),
Config.jsonBody(filteredUser),
{ method: 'PUT' },
])
);
if (res.ok) {
return res.json();
}
} catch (err) {
return false;
}
},

registerUser: async (): Promise<DuosUser> => {
const url = `${await getApiUrl()}/api/user`;
const res = await fetchOk(
url,
fp.mergeAll([Config.authOpts(), { method: 'POST' }])
);
return res.json();
},

getSOsForCurrentUser: async (): Promise<SimplifiedDuosUser[]> => {
const url = `${await getApiUrl()}/api/user/signing-officials`;
const res = await fetchOk(
url,
fp.mergeAll([Config.authOpts(), { method: 'GET' }])
);
return res.json();
},

getUnassignedUsers: async (): Promise<DuosUserResponse[]> => {
const url = `${await getApiUrl()}/api/user/institution/unassigned`;
const res = await axios.get(url, Config.authOpts());
return res.data;
},

addRoleToUser: async (userId: number, roleId: number): Promise<DuosUserResponse> => {
const url = `${await getApiUrl()}/api/user/${userId}/${roleId}`;
const res = await fetchAny(
url,
fp.mergeAll([Config.authOpts(), { method: 'PUT' }])
);
return res.json();
},

deleteRoleFromUser: async (userId: number, roleId: number): Promise<DuosUserResponse> => {
const url = `${await getApiUrl()}/api/user/${userId}/${roleId}`;
const res = await fetchAny(
url,
fp.mergeAll([Config.authOpts(), { method: 'DELETE' }])
);
return res.json();
},


getUserRelevantDatasets: async (): Promise<Dataset[]> => {
const url = `${await getApiUrl()}/api/user/me/dac/datasets`;
const res = await axios.get(url, Config.authOpts());
return res.data;
},

getAcknowledgements: async (): Promise<AcknowledgementMap> => {
const url = `${await getApiUrl()}/api/user/acknowledgements`;
const res = await axios.get(url, Config.authOpts());
return res.data;
},

acceptAcknowledgments: async (...keys: string[]): Promise<AcknowledgementMap> => {
if (keys.length === 0) {
return {};
}

const url = `${await getApiUrl()}/api/user/acknowledgements`;
const res = await axios.post(url, keys, Config.authOpts());
return res.data;
},

getApprovedDatasets: async (): Promise<ApprovedDataset[]> => {
const url = `${await getApiUrl()}/api/user/me/researcher/datasets`;
const res = await axios.get(url, Config.authOpts());
return res.data;
},
};
7 changes: 7 additions & 0 deletions src/libs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ export const convertLabelToKey = (label = '') => {
return label.split(/[\s,]+/).join('-');
};

/**
* Sets the user's role status.
* Converts a DuosUserResponse into a DuosUser.
* @param {DuosUserResponse} user
* @param {*} Storage
* @returns converted DuosUser
*/
export const setUserRoleStatuses = (user, Storage) => {
const currentUserRoles = (user.roles) ? user.roles.map(roles => roles.name) : [];
user.isChairPerson = currentUserRoles.indexOf(USER_ROLES.chairperson) > -1;
Expand Down
Loading

0 comments on commit 2159399

Please sign in to comment.