346 lines
8.7 KiB
TypeScript
346 lines
8.7 KiB
TypeScript
/** @file
|
|
* @author Edouard DUPIN
|
|
* @copyright 2018, Edouard DUPIN, all right reserved
|
|
* @license PROPRIETARY (see license file)
|
|
*/
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import {
|
|
SessionService,
|
|
StorageService,
|
|
HTTPMimeType,
|
|
HTTPRequestModel,
|
|
HttpWrapperService,
|
|
ModelResponseHttp,
|
|
} from 'common/service';
|
|
|
|
interface MessageLogIn {
|
|
login: string;
|
|
method: string;
|
|
time: number;
|
|
password: string;
|
|
}
|
|
|
|
export interface SpecificTokenResponse {
|
|
jwt: string;
|
|
url: string;
|
|
}
|
|
export interface SpecificReturnResponse {
|
|
url: string;
|
|
}
|
|
export interface GetApplicationSmallResponse {
|
|
name: string;
|
|
description: string;
|
|
redirect: string;
|
|
}
|
|
|
|
export interface ApplicationModel {
|
|
id: number;
|
|
name: string;
|
|
description: string;
|
|
redirect: string;
|
|
redirectDev: string;
|
|
notification: string;
|
|
ttl: number;
|
|
}
|
|
export interface ApplicationRightModel {
|
|
id: number;
|
|
applicationId: number;
|
|
title: string;
|
|
description: string;
|
|
key: string;
|
|
type: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class ApplicationService {
|
|
constructor(private http: HttpWrapperService, private sessionService: SessionService) {
|
|
console.log('Start ApplicationService');
|
|
}
|
|
|
|
getApplicationRights(applicationId: number): Promise<ApplicationRightModel[]> {
|
|
const self = this;
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: `application/${applicationId}/rights`,
|
|
requestType: HTTPRequestModel.GET,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
// TODO: check type ...
|
|
console.log(
|
|
`retrieve Token for application : get some data to check: ${JSON.stringify(response.data)}`
|
|
);
|
|
// tODO: check the format...
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
getApplicationSpecificToken(applicationId: string): Promise<SpecificTokenResponse> {
|
|
const self = this;
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: 'application/get_token',
|
|
requestType: HTTPRequestModel.GET,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
params: {
|
|
application: applicationId,
|
|
},
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
// TODO: check type ...
|
|
console.log(
|
|
`retrieve Token for application : get some data to check: ${JSON.stringify(response.data)}`
|
|
);
|
|
// tODO: check the format...
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
|
|
addUser(applicationId: number, userId: number): Promise<boolean> {
|
|
const self = this;
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: `application/${applicationId}/users`,
|
|
requestType: HTTPRequestModel.POST,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
body: {
|
|
userId,
|
|
},
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
console.log(
|
|
`User has been added: ${JSON.stringify(response.data)}`
|
|
);
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
|
|
rmUser(applicationId: number, userId: number): Promise<boolean> {
|
|
const self = this;
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: `application/${applicationId}/users`,
|
|
requestType: HTTPRequestModel.DELETE,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
body: {
|
|
userId,
|
|
},
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
console.log(
|
|
`User has been added: ${JSON.stringify(response.data)}`
|
|
);
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
|
|
getApplicationReturn(applicationId: string): Promise<SpecificReturnResponse> {
|
|
const self = this;
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: 'application/return',
|
|
requestType: HTTPRequestModel.GET,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
params: {
|
|
application: applicationId,
|
|
},
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
// TODO: check type ...
|
|
console.log(
|
|
`retreive return for application : get some data to check: ${JSON.stringify(response.data)}`
|
|
);
|
|
// tODO: check the format...
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
getApplicationsSmall(): Promise<GetApplicationSmallResponse[]> {
|
|
const self = this;
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: 'application/small',
|
|
requestType: HTTPRequestModel.GET,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
// TODO: check type ...
|
|
console.log(
|
|
`retreive return for application : get some data to check: ${JSON.stringify(response.data)}`
|
|
);
|
|
// tODO: check the format...
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
|
|
getUsers(id: number): Promise<number[]> {
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: `application/${id}/users`,
|
|
requestType: HTTPRequestModel.GET,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
// TODO: check type ...
|
|
console.log(
|
|
`retrieve return for application : get some data to check: ${JSON.stringify(response.data)}`
|
|
);
|
|
// TODO: check the format...
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
gets(): Promise<ApplicationModel[]> {
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: 'application',
|
|
requestType: HTTPRequestModel.GET,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
// TODO: check type ...
|
|
console.log(
|
|
`retreive return for application : get some data to check: ${JSON.stringify(response.data)}`
|
|
);
|
|
// tODO: check the format...
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
get(id: number): Promise<ApplicationModel> {
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: `application/${id}`,
|
|
requestType: HTTPRequestModel.GET,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
// TODO: check type ...
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
update(id: number, updateState: object): Promise<ApplicationModel> {
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: `application/${id}`,
|
|
requestType: HTTPRequestModel.PUT,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
body: updateState,
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
// TODO: check type ...
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
|
|
create(name: string, redirect: string): Promise<ApplicationModel> {
|
|
let body = {
|
|
name,
|
|
redirect,
|
|
};
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.requestJson({
|
|
server: 'karso',
|
|
endPoint: 'application',
|
|
requestType: HTTPRequestModel.POST,
|
|
accept: HTTPMimeType.JSON,
|
|
contentType: HTTPMimeType.JSON,
|
|
body,
|
|
})
|
|
.then((response: ModelResponseHttp) => {
|
|
resolve(response.data);
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
remove(id: number): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
this.http
|
|
.request({
|
|
server: 'karso',
|
|
endPoint: `application/${id}`,
|
|
requestType: HTTPRequestModel.DELETE,
|
|
accept: HTTPMimeType.ALL,
|
|
contentType: HTTPMimeType.JSON,
|
|
})
|
|
.then(() => {
|
|
resolve();
|
|
})
|
|
.catch((error: any) => {
|
|
reject(`return ERROR ${JSON.stringify(error, null, 2)}`);
|
|
});
|
|
});
|
|
}
|
|
}
|