refactor: factorise addBadge and getUuidFromBadgeMetadata to one endpoint handling both cases
Signed-off-by: Rbillon59 <r.billon@celonis.com>
This commit is contained in:
@@ -48,13 +48,17 @@ describe('DatabaseBadgesStore', () => {
|
||||
({ knex, badgeStore } = await createDatabaseBadgesStore(databaseId));
|
||||
});
|
||||
|
||||
it('createABadge', async () => {
|
||||
const uuid = await badgeStore.addBadge(
|
||||
it('createABadge if not existing in DB', async () => {
|
||||
const uuid = await badgeStore.getBadgeUuid(
|
||||
entity.metadata.name,
|
||||
entity.metadata.namespace || 'default',
|
||||
entity.kind,
|
||||
);
|
||||
|
||||
expect(uuid.uuid).toMatch(
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
|
||||
);
|
||||
|
||||
const storedBadge = await badgeStore.getBadgeFromUuid(uuid.uuid);
|
||||
expect(storedBadge?.kind).toEqual(entity.kind);
|
||||
expect(storedBadge?.name).toEqual(entity.metadata.name);
|
||||
@@ -63,7 +67,7 @@ describe('DatabaseBadgesStore', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('getBadgeFromUuid', async () => {
|
||||
it('getBadge if badge already exist in DB', async () => {
|
||||
await knex('badges').truncate();
|
||||
await knex('badges').insert([
|
||||
{
|
||||
@@ -83,7 +87,7 @@ describe('DatabaseBadgesStore', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('getUuidFromEntityMetadata', async () => {
|
||||
it('getBadgeUuid if badge exist in DB', async () => {
|
||||
await knex('badges').truncate();
|
||||
await knex('badges').insert([
|
||||
{
|
||||
@@ -94,15 +98,13 @@ describe('DatabaseBadgesStore', () => {
|
||||
},
|
||||
]);
|
||||
|
||||
const storedUuid = await badgeStore.getUuidFromEntityMetadata(
|
||||
const storedUuid = await badgeStore.getBadgeUuid(
|
||||
'test',
|
||||
'default',
|
||||
'component',
|
||||
);
|
||||
|
||||
expect(storedUuid).toEqual({
|
||||
uuid: 'uuid1',
|
||||
});
|
||||
expect(storedUuid).toEqual({ uuid: 'uuid1' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
resolvePackagePath,
|
||||
} from '@backstage/backend-common';
|
||||
import { Knex } from 'knex';
|
||||
import { isNil } from 'lodash';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
/**
|
||||
@@ -26,7 +27,7 @@ import { v4 as uuidv4 } from 'uuid';
|
||||
* @public
|
||||
*/
|
||||
export interface BadgesStore {
|
||||
addBadge(
|
||||
getBadgeUuid(
|
||||
name: string,
|
||||
namespace: string,
|
||||
kind: string,
|
||||
@@ -35,12 +36,6 @@ export interface BadgesStore {
|
||||
getBadgeFromUuid(
|
||||
uuid: string,
|
||||
): Promise<{ name: string; namespace: string; kind: string } | undefined>;
|
||||
|
||||
getUuidFromEntityMetadata(
|
||||
name: string,
|
||||
namespace: string,
|
||||
kind: string,
|
||||
): Promise<{ uuid: string } | undefined>;
|
||||
}
|
||||
|
||||
const migrationsDir = resolvePackagePath(
|
||||
@@ -84,35 +79,31 @@ export class DatabaseBadgesStore implements BadgesStore {
|
||||
return result;
|
||||
}
|
||||
|
||||
async getUuidFromEntityMetadata(
|
||||
async getBadgeUuid(
|
||||
name: string,
|
||||
namespace: string,
|
||||
kind: string,
|
||||
): Promise<{ uuid: string } | undefined> {
|
||||
): Promise<{ uuid: string }> {
|
||||
const result = await this.db('badges')
|
||||
.select('uuid')
|
||||
.where({ name: name, namespace: namespace, kind: kind })
|
||||
.first();
|
||||
|
||||
return result;
|
||||
}
|
||||
let uuid = result?.uuid;
|
||||
|
||||
async addBadge(
|
||||
name: string,
|
||||
namespace: string,
|
||||
kind: string,
|
||||
): Promise<{ uuid: string }> {
|
||||
const uuid = uuidv4();
|
||||
if (isNil(uuid)) {
|
||||
uuid = uuidv4();
|
||||
|
||||
await this.db('badges')
|
||||
.insert({
|
||||
uuid: uuid,
|
||||
name: name,
|
||||
namespace: namespace,
|
||||
kind: kind,
|
||||
})
|
||||
.onConflict(['name', 'namespace', 'kind'])
|
||||
.ignore();
|
||||
await this.db('badges')
|
||||
.insert({
|
||||
uuid: uuid,
|
||||
name: name,
|
||||
namespace: namespace,
|
||||
kind: kind,
|
||||
})
|
||||
.onConflict(['name', 'namespace', 'kind'])
|
||||
.ignore();
|
||||
}
|
||||
|
||||
return { uuid };
|
||||
}
|
||||
|
||||
@@ -129,15 +129,12 @@ describe('createRouter', () => {
|
||||
};
|
||||
|
||||
const badgeStore: jest.Mocked<BadgesStore> = {
|
||||
addBadge: jest.fn().mockImplementation(async () => {
|
||||
getBadgeUuid: jest.fn().mockImplementation(async () => {
|
||||
return { uuid: 'uuid1' };
|
||||
}),
|
||||
getBadgeFromUuid: jest.fn().mockImplementation(async () => {
|
||||
return badgeEntity;
|
||||
}),
|
||||
getUuidFromEntityMetadata: jest
|
||||
.fn()
|
||||
.mockImplementation(async () => 'uuid1'),
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
|
||||
@@ -76,8 +76,7 @@ describe('createRouter', () => {
|
||||
|
||||
const badgeStore: jest.Mocked<BadgesStore> = {
|
||||
getBadgeFromUuid: jest.fn(),
|
||||
getUuidFromEntityMetadata: jest.fn(),
|
||||
addBadge: jest.fn(),
|
||||
getBadgeUuid: jest.fn(),
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
|
||||
@@ -233,17 +233,13 @@ async function obfuscatedRoute(
|
||||
},
|
||||
async (req, res) => {
|
||||
const { namespace, kind, name } = req.params;
|
||||
let storedEntityUuid: { uuid: string } | undefined =
|
||||
await store.getUuidFromEntityMetadata(name, namespace, kind);
|
||||
const storedEntityUuid: { uuid: string } | undefined =
|
||||
await store.getBadgeUuid(name, namespace, kind);
|
||||
|
||||
if (isNil(storedEntityUuid)) {
|
||||
storedEntityUuid = await store.addBadge(name, namespace, kind);
|
||||
|
||||
if (isNil(storedEntityUuid)) {
|
||||
throw new NotFoundError(
|
||||
`No uuid found for entity "${namespace}/${kind}/${name}"`,
|
||||
);
|
||||
}
|
||||
throw new NotFoundError(
|
||||
`No uuid found for entity "${namespace}/${kind}/${name}"`,
|
||||
);
|
||||
}
|
||||
|
||||
return res.status(200).json(storedEntityUuid);
|
||||
|
||||
Reference in New Issue
Block a user