diff --git a/src/notes/utils.spec.ts b/src/notes/utils.spec.ts index 6fb90d12c..55bc5e302 100644 --- a/src/notes/utils.spec.ts +++ b/src/notes/utils.spec.ts @@ -5,19 +5,36 @@ */ import { randomBytes } from 'crypto'; -import { generatePublicId } from './utils'; +import { User } from '../users/user.entity'; +import { Alias } from './alias.entity'; +import { Note } from './note.entity'; +import { generatePublicId, getPrimaryAlias } from './utils'; jest.mock('crypto'); +const random128bitBuffer = Buffer.from([ + 0xe1, 0x75, 0x86, 0xb7, 0xc3, 0xfb, 0x03, 0xa9, 0x26, 0x9f, 0xc9, 0xd6, 0x8c, + 0x2d, 0x7b, 0x7b, +]); +const mockRandomBytes = randomBytes as jest.MockedFunction; +mockRandomBytes.mockImplementation((_) => random128bitBuffer); it('generatePublicId', () => { - const random128bitBuffer = Buffer.from([ - 0xe1, 0x75, 0x86, 0xb7, 0xc3, 0xfb, 0x03, 0xa9, 0x26, 0x9f, 0xc9, 0xd6, - 0x8c, 0x2d, 0x7b, 0x7b, - ]); - const mockRandomBytes = randomBytes as jest.MockedFunction< - typeof randomBytes - >; - mockRandomBytes.mockImplementationOnce((_) => random128bitBuffer); - expect(generatePublicId()).toEqual('w5trddy3zc1tj9mzs7b8rbbvfc'); }); + +describe('getPrimaryAlias', () => { + const alias = 'alias'; + let note: Note; + beforeEach(() => { + const user = User.create('hardcoded', 'Testy') as User; + note = Note.create(user, alias); + }); + it('finds correct primary alias', () => { + note.aliases.push(Alias.create('annother', false)); + expect(getPrimaryAlias(note)).toEqual(alias); + }); + it('returns undefined if there is no alias', () => { + note.aliases[0].primary = false; + expect(getPrimaryAlias(note)).toEqual(undefined); + }); +}); diff --git a/src/notes/utils.ts b/src/notes/utils.ts index 7d0e75c01..7f6229fe7 100644 --- a/src/notes/utils.ts +++ b/src/notes/utils.ts @@ -6,6 +6,9 @@ import base32Encode from 'base32-encode'; import { randomBytes } from 'crypto'; +import { Alias } from './alias.entity'; +import { Note } from './note.entity'; + /** * Generate publicId for a note. * This is a randomly generated 128-bit value encoded with base32-encode using the crockford variant and converted to lowercase. @@ -14,3 +17,17 @@ export function generatePublicId(): string { const randomId = randomBytes(16); return base32Encode(randomId, 'Crockford').toLowerCase(); } + +/** + * Extract the primary alias from a aliases of a note + * @param {Note} note - the note from which the primary alias should be extracted + */ +export function getPrimaryAlias(note: Note): string | undefined { + const listWithPrimaryAlias = note.aliases.filter( + (alias: Alias) => alias.primary, + ); + if (listWithPrimaryAlias.length !== 1) { + return undefined; + } + return listWithPrimaryAlias[0].name; +}