Skip to main content

Báo lỗi

Mô hình dữ liệu

Mối quan hệ với các mô hình khác

/src/app/services/invidual-report-v2/invidual-report-v2.service.ts
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { catchError, firstValueFrom } from "rxjs";
import { ApiResponse } from "src/app/models/api/api-response.model";
import { BaseService } from "../base/base.service";
import { ConfigService } from "../config/config.service";
import { LoadingService } from "../loading/loading-service";
import { MessageService } from "../message/message.service";
import { CacheManagerService } from "../cache-manager/cache-manager.service";
import { InvidualReport_v2_PostModel } from "src/app/models/invidual_report_v2/invidual-report-v2.post.model";
import { InvidualReportResponseModel_v2, InvidualReport_v2 } from "src/app/models/invidual_report_v2/invidual_report_v2.model";
import { InvidualReportImage_v2_PostModel } from "src/app/models/invidual_report_v2/invidual_report_v2-image.post.model";
import { ImageResponseModel } from "src/app/models/image/image.response.model";
import { Image } from "src/app/models/image/image.model";
import { InvidualReportStatus } from "src/app/models/invidual_report_v2/invidual_report_status.enum";
import { TimeService } from "../time/time.service";
import { PaginationResponseModel } from "src/app/models/pagination/pagination.response.model";
import { PaginationData } from "src/app/models/pagination/pagination-data.model";
import { InvidualReportActionPostModel } from "src/app/models/invidual_report_v2/invidual-report-action.post.model";
import { InvidualReportAction, InvidualReportActionResponseModel } from "src/app/models/invidual_report_v2/invidual-report-action.model";
import { FileResponseModel } from "src/app/models/file/file.response.model";
import { FileModel } from "src/app/models/file/file.model";
import { FilesService } from "../file/files.service";
import { InvidualReport_v2_ExportPostModel } from "src/app/models/invidual_report_v2/invidual-report-v2-export.post.model";
import { InvidualReportStat_v2_SerializedModel } from "src/app/models/invidual_report_v2/invidual-report-stat-v2.serialized.model";
import { InvidualReportStatIndices_v2_SerializedModel } from "src/app/models/invidual_report_v2/invidual-report-stat-indices-v2.serialized.model";
import { InvidualReportStatPIC_v2_SerializedModel } from "src/app/models/invidual_report_v2/invidual-report-stat-pic-v2.serialized.model";
import { InvidualReport_v2_PutModel } from "src/app/models/invidual_report_v2/invidual-report-v2.put.model";

@Injectable()
export class InvidualReport_v2_Service extends BaseService {

constructor(
private http: HttpClient
, private messageService: MessageService
, private loadingService: LoadingService
, private configService: ConfigService
, private cacheManagerService: CacheManagerService
, private timeService: TimeService
, private filesService: FilesService
)
{
super();
this.baseAddress = `[fmApi]/v2/[clientId]/[projectId]/invidual-reports`;
this.baseCMAddress = `[cmApi]/v1/images/invidual-reports`;

}

private baseAddress: string;
private baseCMAddress: string;
private httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};

private _clearLocalCache(route: string = "")
{
if(route && route.length > 0)
{
this.cacheManagerService.clearLocalCacheWithNameIncluding(["/api/", "/v2/", "/invidual-reports", route]);
}
else
{
this.cacheManagerService.clearLocalCacheWithNameIncluding(["/api/", "/v2/", "/invidual-reports"]);
}
}

private _clearSingleLocalCache(reportId: string)
{
if(reportId && reportId.length > 0)
{
this.cacheManagerService.clearLocalCacheWithNameIncluding(["/api/", "/v2/", "/invidual-reports", "single-" + reportId]);
}
}

private _clearFileLocalCache(reportId: string)
{
if(reportId && reportId.length > 0)
{
this.cacheManagerService.clearLocalCacheWithNameIncluding(["/api/", "/v2/", "/invidual-reports", "files-" + reportId]);
}
}

private _clearLocalActionCache(reportId: string = "")
{
if(reportId && reportId.length > 0)
{
this.cacheManagerService.clearLocalCacheWithNameIncluding(["/api/", "/v2/", "/invidual-report-actions", "/host-" + reportId]);
}
else
{
this.cacheManagerService.clearLocalCacheWithNameIncluding(["/api/", "/v2/", "/invidual-report-actions"]);
}
}

public async gets(
spaceId: string, text: string,
statuses: InvidualReportStatus[], types: string[],
from: number, to: number,
pageIndex: number, forceNew: boolean = false, loadingDivId: string = "")
{
try
{
this.loadingService.show("", -1, loadingDivId);
if(!text || text.length < 3) text = "";
if(statuses && statuses.length > 0) statuses = statuses.sort((a, b) => a - b);
const status: string = statuses && statuses.length > 0 ? Object.values(statuses).join(",") : "";
const type: string = types && types.length > 0 ? types.sort((a, b) => a > b ? -1 : 1).join(",") : "";

if(from > 0)
{
from = this.timeService.roundMinutes(from, 10);
}

if(to > 0)
{
if(to > new Date().getTime()) to = new Date().getTime();
to = this.timeService.roundMinutes(to, 10);
}

const response = await firstValueFrom(this.http.get<ApiResponse<PaginationResponseModel<InvidualReportResponseModel_v2>>>(`${this.baseAddress}?from=${from}&page=${pageIndex}&spaceId=${spaceId}&status=${status}&text=${text}&to=${to}&type=${type}`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 24 * 60, forceNew)
)
.pipe(
catchError(this.handleError)
));

this.loadingService.hide(loadingDivId);

if(response && response.result)
{
const items = response.result.items.map(x => new InvidualReport_v2(x));
return new PaginationData(items, response.result.total);
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err)
{
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async post(
model: InvidualReport_v2_PostModel,
loadingDivId: string = ""
)
{
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.post<ApiResponse<InvidualReportResponseModel_v2>>(`${this.baseAddress}`, model, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if(response.result)
{
this._clearLocalCache();
this.messageService.setMessage("Successfully");
const item = new InvidualReport_v2(response.result);
return item;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err)
{
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async put(
model: InvidualReport_v2_PutModel,
loadingDivId: string = ""
)
{
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.put<ApiResponse<InvidualReportResponseModel_v2>>(`${this.baseAddress}`, model, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if(response.result)
{
this._clearLocalCache();
this.messageService.setMessage("successfully");
const item = new InvidualReport_v2(response.result);
return item;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err)
{
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async postReportImages(
model: InvidualReportImage_v2_PostModel,
loadingDivId: string = ""
)
{
this.messageService.clearMessage();

try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.post<ApiResponse<ImageResponseModel[]>>(`${this.baseCMAddress}/invidual-reports`, model, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if(response?.result)
{
this.messageService.setMessage("Uploaded images: " + response.result.length);
const items = response.result.map(x => new Image(x, this.configService.appConfig.CM_URL.URL, true));
return items;
}
else
{
this.messageService.setErrorMessage("Error: Can not upload the images, you can add images later to the created report.");
//this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
console.error(err);
this.loadingService.hide(loadingDivId);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}


public async action(
model: InvidualReportActionPostModel,
loadingDivId: string = ""
)
{
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.post<ApiResponse<InvidualReportActionResponseModel[]>>(`${this.baseAddress}/actions`, model, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if(response.result)
{
this._clearLocalCache();
this._clearSingleLocalCache(model.reportId);
this._clearLocalActionCache(model.reportId);

this.messageService.setMessage("Successfully");
if(response.result.length > 0)
{
const items = response.result.map(x => new InvidualReportAction(x));
return items;
}
return [];
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err)
{
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async getsFile(
report: InvidualReport_v2,
forceNew: boolean = false,
loadingDivId: string = ""
)
{
try
{
this.loadingService.show("", -1, loadingDivId);

const response = await firstValueFrom(this.http.get<ApiResponse<FileResponseModel[]>>(`${this.baseAddress}/files-${report.id}`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 24 * 60, forceNew)
)
.pipe(
catchError(this.handleError)
));

this.loadingService.hide(loadingDivId);

if(response && response.result)
{
if(response.result.length > 0)
{
report.files = response.result.map(x => new FileModel(x));
}
else
{
report.files = [];
}

return report.files;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err)
{
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}


//#region Export
public async getExports(
pageIndex: number,
forceNew: boolean = false,
loadingName: string = ""
)
{
this.messageService.clearMessage();
try
{
this.loadingService.show("", -1, loadingName);

const response = await firstValueFrom(this.http.get<ApiResponse<PaginationResponseModel<FileResponseModel>>>(`${this.baseAddress}/exports?page=${pageIndex}`,
this.cacheManagerService.getClientCacheHttpOptions(this.httpOptions.headers, 60, forceNew)
)
.pipe(
catchError(this.handleError)
));

this.loadingService.hide(loadingName);
if(response.result)
{
const items = response.result.items.map(x => new FileModel(x));
const pagination = new PaginationData(items, response.result.total);
return pagination;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
}
return false;
}
catch(err)
{
this.loadingService.hide(loadingName);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async postExports(
model: InvidualReport_v2_ExportPostModel,
loadingName: string = ""
)
{
this.messageService.clearMessage();
try
{
this.loadingService.show("", -1, loadingName);

const response = await firstValueFrom(this.http.post<ApiResponse<boolean>>(`${this.baseAddress}/exports`, model, this.httpOptions)
.pipe(
catchError(this.handleError)
));

this.loadingService.hide(loadingName);
if(response.result)
{
this.messageService.setMessage("Your exporting task has been queued and will notice you when finish.");
return true;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
}
return false;
}
catch(err)
{
this.loadingService.hide(loadingName);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}


public async viewExport(item: FileModel)
{
this.messageService.clearMessage();
try
{
this.loadingService.show();
const response = await firstValueFrom(this.http.get(`${this.baseAddress}/exports/view-${item.id}`, {
responseType : 'blob'
})
.pipe(
catchError(this.handleError)
));

this.loadingService.hide();

await this.filesService.viewFileFromBlob(response, item.originalFileName);
return true;
}
catch(err)
{
console.error(err);
this.loadingService.hide();
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}
//#endregion


public async getByMonthStats(from: number, to: number, forceNew: boolean = false, loadingName: string = "")
{
this.messageService.clearMessage();
try
{
if(from < 0 || from > to || to > new Date().getTime())
{
this.messageService.setErrorMessage("Invalid information.");
return false;
}

from = this.timeService.roundMinutes(from, 60);
to = this.timeService.roundMinutes(to, 60);

this.loadingService.show("", -1, loadingName);

const response = await firstValueFrom(this.http.get<ApiResponse<InvidualReportStat_v2_SerializedModel[]>>(`${this.baseAddress}/stats/by-months?from=${from}&to=${to}`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 24 * 60, forceNew)
)
.pipe(
catchError(this.handleError)
));

this.loadingService.hide(loadingName);
if(response.result)
{
return response.result;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
}
return false;
}
catch(err)
{
this.loadingService.hide(loadingName);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async getByMonthCategoryStats(from: number, to: number, forceNew: boolean = false, loadingName: string = "")
{
this.messageService.clearMessage();
try
{
if(from < 0 || from > to || to > new Date().getTime())
{
this.messageService.setErrorMessage("Invalid information.");
return false;
}

from = this.timeService.roundMinutes(from, 60);
to = this.timeService.roundMinutes(to, 60);

this.loadingService.show("", -1, loadingName);

const response = await firstValueFrom(this.http.get<ApiResponse<InvidualReportStat_v2_SerializedModel[]>>(`${this.baseAddress}/stats/categories/by-months?from=${from}&to=${to}`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 24 * 60, forceNew)
)
.pipe(
catchError(this.handleError)
));

this.loadingService.hide(loadingName);
if(response.result)
{
return response.result;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
}
return false;
}
catch(err)
{
this.loadingService.hide(loadingName);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async getStatIndices(from: number, to: number, forceNew: boolean = false, loadingName: string = "")
{
this.messageService.clearMessage();
try
{
if(from < 0 || from > to || to > new Date().getTime())
{
this.messageService.setErrorMessage("Invalid information.");
return false;
}

from = this.timeService.roundMinutes(from, 60);
to = this.timeService.roundMinutes(to, 60);

this.loadingService.show("", -1, loadingName);

const response = await firstValueFrom(this.http.get<ApiResponse<InvidualReportStatIndices_v2_SerializedModel>>(`${this.baseAddress}/stats/indices?from=${from}&to=${to}`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 24 * 60, forceNew)
)
.pipe(
catchError(this.handleError)
));

this.loadingService.hide(loadingName);
if(response.result)
{
return response.result;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
}
return false;
}
catch(err)
{
this.loadingService.hide(loadingName);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async getStatPICs(from: number, to: number, forceNew: boolean = false, loadingName: string = "")
{
this.messageService.clearMessage();
try
{
if(from < 0 || from > to || to > new Date().getTime())
{
this.messageService.setErrorMessage("Invalid information.");
return false;
}

from = this.timeService.roundMinutes(from, 60);
to = this.timeService.roundMinutes(to, 60);

this.loadingService.show("", -1, loadingName);

const response = await firstValueFrom(this.http.get<ApiResponse<InvidualReportStatPIC_v2_SerializedModel>>(`${this.baseAddress}/stats/pics?from=${from}&to=${to}`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 24 * 60, forceNew)
)
.pipe(
catchError(this.handleError)
));

this.loadingService.hide(loadingName);
if(response.result)
{
return response.result;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
}
return false;
}
catch(err)
{
this.loadingService.hide(loadingName);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

}