Skip to content

Commit

Permalink
added some mpi endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-krenz committed Sep 13, 2024
1 parent 9c23f40 commit f849e19
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
# Scripts
/scripts/node_modules/
/scripts/dist/

/.vscode/
47 changes: 43 additions & 4 deletions src/mpi/mpi.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Controller, Get, Query } from '@nestjs/common';
import { Controller, Get, Query, Post, Body } from '@nestjs/common';
import { MPIService } from './mpi.service';

type eLabsStatus = 'PENDING' | 'PROGRESS' | 'COMPLETED';

@Controller('mpi')
export class MPIController {
constructor(private readonly mpiService: MPIService) {}
Expand All @@ -10,13 +12,50 @@ export class MPIController {
return await this.mpiService.exchangeCodeForToken(code);
}

@Get('sequences')
async getSequences(): Promise<string> {
return this.mpiService.getSequences();
@Get('is_logged_in')
isLoggedIn(): { loggedIn: boolean } {
return { loggedIn: this.mpiService.isLoggedIn() };
}

@Get('auth0_logout')
async logout(): Promise<string> {
return this.mpiService.logout();
}

@Get('sequences')
async getSequences(): Promise<string> {
return this.mpiService.getSequences();
}

@Get('azentaSeqOrder/:id')
async azentaSeqOrder(id: string): Promise<string> {
return this.mpiService.azentaSeqOrder(id);
}

@Get('azentaSeqOrders')
async azentaSeqOrders(): Promise<string> {
return this.mpiService.azentaSeqOrders();
}

@Get('azentaCreateSeqOrder')
async azentaCreateSeqOrder(): Promise<string> {
return this.mpiService.azentaCreateSeqOrder();
}

@Post('e-labs/create-study')
async createELabsStudy(@Body('bearerToken') bearerToken: string, @Body('projectID') projectID: number, @Body('name') name: string): Promise<number | undefined> {
return this.mpiService.createELabsStudy(bearerToken, projectID, name);
}

@Post('e-labs/create-experiment')
async createELabsExperiment(
@Body('bearerToken') bearerToken: string,
@Body('studyID') studyID: number,
@Body('name') name: string,
@Body('status') status: eLabsStatus,
@Body('templateID') templateID?: number,
@Body('autoCollaborate') autoCollaborate?: boolean
): Promise<number | undefined> {
return this.mpiService.createELabsExperiment(bearerToken, studyID, name, status, templateID, autoCollaborate);
}
}
160 changes: 156 additions & 4 deletions src/mpi/mpi.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import axios from 'axios';

// type ELabsStudyResponse = {
// studyID : number;
// projectID : number;
// groupID : number;
// subGroupID : number;
// userID : number;
// name : string;
// statusChanged: string;
// description : string;
// notes : string;
// approve : string;
// created : string;
// deleted : boolean;
// };

type eLabsStatus = 'PENDING' | 'PROGRESS' | 'COMPLETED';

// type ELabsExperimentResponse = {
// bearerToken : string,
// experimentID : number,
// studyID : number,
// name : string,
// status : eLabsStatus,
// templateID? : number,
// autoCollaborate?: boolean
// }

@Injectable()
export class MPIService {
private tokenStore = new Map<string, { accessToken: string; refreshToken: string; accessTokenExpiration: Date }>();
Expand All @@ -13,10 +40,10 @@ export class MPIService {
private currentUserId = 'mpitest';

// To log in use:
// NOTE THAT THIS SERVER IS RUNNING ON PORT 3000
// https://mpi-dev.us.auth0.com/authorize?response_type=code&scope=offline_access&client_id=tZSXM9f8WUiPIpNGt1kXlGqzZVYvWNEF&redirect_uri=http://127.0.0.1:3000/mpi/auth0_redirect&audience=https://mpi.com
// NOTE THAT THIS SERVER IS RUNNING ON PORT 5100
// https://mpi-dev.us.auth0.com/authorize?response_type=code&scope=offline_access&client_id=tZSXM9f8WUiPIpNGt1kXlGqzZVYvWNEF&redirect_uri=http://127.0.0.1:5100/mpi/auth0_redirect&audience=https://mpi.com
// To log out use:
// https://mpi-dev.us.auth0.com/oidc/logout?post_logout_redirect_uri=http://127.0.0.1:3000/mpi/auth0_logout&client_id=tZSXM9f8WUiPIpNGt1kXlGqzZVYvWNEF
// https://mpi-dev.us.auth0.com/oidc/logout?post_logout_redirect_uri=http://127.0.0.1:5100/mpi/auth0_logout&client_id=tZSXM9f8WUiPIpNGt1kXlGqzZVYvWNEF

async exchangeCodeForToken(code: string): Promise<string> {
const tokenUrl = `https://mpi-dev.us.auth0.com/oauth/token`;
Expand All @@ -25,7 +52,7 @@ export class MPIService {
client_id: this.client_id,
client_secret: this.client_secret,
code,
redirect_uri: 'http://127.0.0.1:3000/mpi/auth0_redirect' // URL in this server that was used to redirect to after Auth0 login
redirect_uri: 'http://127.0.0.1:5100/mpi/auth0_redirect' // URL in this server that was used to redirect to after Auth0 login
};

const response = await axios.post(tokenUrl, payload, {
Expand Down Expand Up @@ -85,15 +112,39 @@ export class MPIService {
const user = this.tokenStore.get(this.currentUserId);
if (user && user.accessToken && user.accessTokenExpiration) {
if (user.accessTokenExpiration > new Date(Date.now())) {
console.log('user ', user, ' has valid token');
return user.accessToken;
} else {
console.log('user ', user, ' has expired token; requesting refreshed token');
await this.exchangeRefreshTokenForToken();
return this.tokenStore.get(this.currentUserId)!.accessToken;
}
}
return '';
}

isLoggedIn(): boolean {
const user = this.tokenStore.get(this.currentUserId);
if (user && user.accessToken && user.accessTokenExpiration) {
console.log('user ', user, ' is already logged in');
return user.accessTokenExpiration > new Date();
}
return false;
}

async createSequence(): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
}
const sequences = await axios.get('http://localhost:5000/sequences', {
headers: {
Authorization: `Bearer ${token}`
}
});
return sequences.data;
}

async getSequences(): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
Expand All @@ -106,4 +157,105 @@ export class MPIService {
});
return sequences.data;
}

// Azenta endpoints
async azentaSeqOrder(id: string): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
}
const azentaSeqOrder = await axios.get(`http://localhost:5000/azenta/seqOrder/${id}`, {
headers: {
Authorization: `Bearer ${token}`
}
});
return azentaSeqOrder.data;
}

async azentaSeqOrders(): Promise<any> {
const token = await this.getAccessToken();
if (!token) {
return new BadRequestException('No token found, log in to MPI first');
}
const azentaSeqOrders = await axios.get('http://localhost:5000/azenta/seqOrder', {
headers: {
Authorization: `Bearer ${token}`
}
});
return azentaSeqOrders.data;
}

async azentaCreateSeqOrder(): Promise<any> {
const token = await this.getAccessToken();
const order = {
orderName: 'DAMP_Azenta_Order'
};
if (!token) {
throw new BadRequestException('No token found, log in to MPI first');
}
const azentaSeqOrders = await axios.post('http://localhost:5000/azenta/seqOrder', order, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
return azentaSeqOrders.data;
}

// eLabs
async createELabsStudy(bearerToken: string, projectID: number, name: string): Promise<number | undefined> {
const token = await this.getAccessToken();
if (!token) {
throw new BadRequestException('No token found, log in to MPI first');
}
try {
const response = await axios.post(
'http://localhost:5000/e-labs/create-study',
{ bearerToken, projectID, name },
{
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json; charset=utf-8'
}
}
);

if (!response.data || !response.data.studyID) {
throw new Error('Error creating eLabs Study from Canvas Workflow...');
}

return response.data.studyID;
} catch (error) {
console.error(error);
return undefined;
}
}

async createELabsExperiment(bearerToken: string, studyID: number, name: string, status: eLabsStatus, templateID?: number, autoCollaborate?: boolean): Promise<number | undefined> {
const token = await this.getAccessToken();
if (!token) {
throw new BadRequestException('No token found, log in to MPI first');
}
try {
const response = await axios.post(
'http://localhost:5000/e-labs/create-experiment',
{ bearerToken, studyID, name, status, templateID, autoCollaborate },
{
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json; charset=utf-8'
}
}
);

if (!response.data || !response.data.experimentID) {
throw new Error('Error creating eLabs Experiment from Canvas Service...');
}

return response.data.experimentID;
} catch (error) {
console.error(error);
return undefined;
}
}
}

0 comments on commit f849e19

Please sign in to comment.