шлется на реальный сервер (#2)
build-and-deploy / prepare (push) Successful in 0s
build-and-deploy / build-and-push (., Dockerfile, hr-appeals-web) (push) Successful in 8s
Tests / frontend-test (push) Successful in 11s
build-and-deploy / build-and-push (server, server/Dockerfile, hr-appeals-auth) (push) Successful in 4s
build-and-deploy / deploy (push) Successful in 15s
build-and-deploy / prepare (push) Successful in 0s
build-and-deploy / build-and-push (., Dockerfile, hr-appeals-web) (push) Successful in 8s
Tests / frontend-test (push) Successful in 11s
build-and-deploy / build-and-push (server, server/Dockerfile, hr-appeals-auth) (push) Successful in 4s
build-and-deploy / deploy (push) Successful in 15s
Co-authored-by: Mariya Trishina <trishina.mariya@tnsu.kz> Reviewed-on: http://git.tnsu.kz/gorbenko.pavel/hr-appeals/pulls/2
This commit was merged in pull request #2.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { getAppOnlyToken } from './graphAuth';
|
||||
|
||||
const COMPANIES_URL = 'https://api.tnsu.kz/Dictionary/Companies';
|
||||
|
||||
// Справочник компаний меняется редко (новое юрлицо в группе), но перезапуск
|
||||
// сервиса ради этого — лишнее, поэтому кэш с коротким временем жизни.
|
||||
const CACHE_TTL_MS = 10 * 60 * 1000;
|
||||
|
||||
export interface ICompany {
|
||||
Id: string;
|
||||
Name: string;
|
||||
}
|
||||
|
||||
let cached: { companies: ICompany[]; expiresAt: number } | null = null;
|
||||
|
||||
export async function getCompanies(): Promise<ICompany[]> {
|
||||
if (cached && cached.expiresAt > Date.now()) {
|
||||
return cached.companies;
|
||||
}
|
||||
|
||||
const token = await getAppOnlyToken();
|
||||
const response = await fetch(COMPANIES_URL, {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Не удалось получить справочник компаний: ${response.status} ${await response.text()}`
|
||||
);
|
||||
}
|
||||
|
||||
const companies = (await response.json()) as ICompany[];
|
||||
cached = { companies, expiresAt: Date.now() + CACHE_TTL_MS };
|
||||
return companies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Колонка Company в списке текстовая и ждёт название, а форма присылает Id из
|
||||
* справочника. Если Id неизвестен — возвращаем его как есть: потерять обращение
|
||||
* из-за рассинхрона справочника хуже, чем записать в колонку сырой Id.
|
||||
*/
|
||||
export async function resolveCompanyName(id: string): Promise<string> {
|
||||
const companies = await getCompanies();
|
||||
const found = companies.find((company) => company.Id === id);
|
||||
|
||||
if (!found) {
|
||||
console.warn(`Неизвестный Id компании из формы, пишу как есть: ${id}`);
|
||||
return id;
|
||||
}
|
||||
|
||||
return found.Name;
|
||||
}
|
||||
Reference in New Issue
Block a user