From 77c0f97c8ac36435be5bece7a53816960bfb5f8a Mon Sep 17 00:00:00 2001 From: Nick Phura Date: Mon, 25 Nov 2024 11:33:09 -0800 Subject: [PATCH] Add markdown database model files. --- api/src/database-models/markdown.ts | 36 ++++++++++++++++++++ api/src/database-models/markdown_type.ts | 34 ++++++++++++++++++ api/src/database-models/markdown_user.ts | 34 ++++++++++++++++++ api/src/models/markdown-view.ts | 18 ++++------ api/src/openapi/schemas/markdown.ts | 12 +++++-- api/src/paths/markdown/index.ts | 4 +-- api/src/paths/markdown/{markdownId}/index.ts | 13 ++++--- api/src/repositories/markdown-repository.ts | 9 ++--- api/src/services/markdown-service.ts | 7 ++-- 9 files changed, 138 insertions(+), 29 deletions(-) create mode 100644 api/src/database-models/markdown.ts create mode 100644 api/src/database-models/markdown_type.ts create mode 100644 api/src/database-models/markdown_user.ts diff --git a/api/src/database-models/markdown.ts b/api/src/database-models/markdown.ts new file mode 100644 index 0000000000..e13f240989 --- /dev/null +++ b/api/src/database-models/markdown.ts @@ -0,0 +1,36 @@ +import { z } from 'zod'; + +/** + * Markdown Model. + * + * @description Data model for `markdown`. + */ +export const MarkdownModel = z.object({ + markdown_id: z.number(), + markdown_type_id: z.number(), + data: z.string().nullable(), + score: z.number(), + record_end_date: z.string(), + create_date: z.string(), + create_user: z.number(), + update_date: z.string().nullable(), + update_user: z.number().nullable(), + revision_count: z.number() +}); + +export type MarkdownModel = z.infer; + +/** + * Markdown Record. + * + * @description Data record for `markdown`. + */ +export const MarkdownRecord = MarkdownModel.omit({ + create_date: true, + create_user: true, + update_date: true, + update_user: true, + revision_count: true +}); + +export type MarkdownRecord = z.infer; diff --git a/api/src/database-models/markdown_type.ts b/api/src/database-models/markdown_type.ts new file mode 100644 index 0000000000..d40df8b6ed --- /dev/null +++ b/api/src/database-models/markdown_type.ts @@ -0,0 +1,34 @@ +import { z } from 'zod'; + +/** + * Markdown Type Model. + * + * @description Data model for `markdown_type`. + */ +export const MarkdownTypeModel = z.object({ + markdown_type_id: z.number(), + name: z.string(), + description: z.string(), + create_date: z.string(), + create_user: z.number(), + update_date: z.string().nullable(), + update_user: z.number().nullable(), + revision_count: z.number() +}); + +export type MarkdownTypeModel = z.infer; + +/** + * Markdown Type Record. + * + * @description Data record for `markdown_type`. + */ +export const MarkdownTypeRecord = MarkdownTypeModel.omit({ + create_date: true, + create_user: true, + update_date: true, + update_user: true, + revision_count: true +}); + +export type MarkdownTypeRecord = z.infer; diff --git a/api/src/database-models/markdown_user.ts b/api/src/database-models/markdown_user.ts new file mode 100644 index 0000000000..401421d6f6 --- /dev/null +++ b/api/src/database-models/markdown_user.ts @@ -0,0 +1,34 @@ +import { z } from 'zod'; + +/** + * Markdown User Model. + * + * @description Data model for `markdown_user`. + */ +export const MarkdownUserModel = z.object({ + markdown_user_id: z.number(), + system_user_id: z.number(), + markdown_id: z.number(), + create_date: z.string(), + create_user: z.number(), + update_date: z.string().nullable(), + update_user: z.number().nullable(), + revision_count: z.number() +}); + +export type MarkdownUserModel = z.infer; + +/** + * Markdown User Record. + * + * @description Data record for `markdown_user`. + */ +export const MarkdownUserRecord = MarkdownUserModel.omit({ + create_date: true, + create_user: true, + update_date: true, + update_user: true, + revision_count: true +}); + +export type MarkdownUserRecord = z.infer; diff --git a/api/src/models/markdown-view.ts b/api/src/models/markdown-view.ts index 20942dad66..c8a70963af 100644 --- a/api/src/models/markdown-view.ts +++ b/api/src/models/markdown-view.ts @@ -1,22 +1,16 @@ import { z } from 'zod'; +import { MarkdownRecord } from '../database-models/markdown'; -export const MarkdownObject = z.object({ - markdown_id: z.number(), - markdown_type_id: z.number(), - data: z.string(), +export const MarkdownObject = MarkdownRecord.pick({ + markdown_id: true, + markdown_type_id: true, + data: true +}).extend({ participated: z.boolean() }); export type MarkdownObject = z.infer; -export const MarkdownUserObject = z.object({ - markdown_user_id: z.number(), - system_user_id: z.number(), - markdown_id: z.number() -}); - -export type MarkdownUserObject = z.infer; - export interface MarkdownQueryObject { system_user_id: number; markdown_type_name: string; diff --git a/api/src/openapi/schemas/markdown.ts b/api/src/openapi/schemas/markdown.ts index ab9df30314..a4141f6858 100644 --- a/api/src/openapi/schemas/markdown.ts +++ b/api/src/openapi/schemas/markdown.ts @@ -8,6 +8,7 @@ export const markdownSchema: OpenAPIV3.SchemaObject = { type: 'object', description: 'Schema for get markdown response', additionalProperties: false, + required: ['markdown'], properties: { markdown: { type: 'object', @@ -15,13 +16,20 @@ export const markdownSchema: OpenAPIV3.SchemaObject = { required: ['markdown_id', 'markdown_type_id', 'data', 'participated'], additionalProperties: false, properties: { - markdown_id: { type: 'number', description: 'Primary key of the markdown record', minimum: 1 }, + markdown_id: { + type: 'number', + description: 'Primary key of the markdown record', + minimum: 1 + }, markdown_type_id: { type: 'number', description: 'Type of the markdown record, used to identify which records correspond to which dialogs', minimum: 1 }, - data: { type: 'string', description: 'Markdown string to display' }, + data: { + type: 'string', + description: 'Markdown string to display' + }, participated: { type: 'boolean', description: 'True if the user has already scored the markdown record, otherwise false.' diff --git a/api/src/paths/markdown/index.ts b/api/src/paths/markdown/index.ts index d053911080..a525f96d3a 100644 --- a/api/src/paths/markdown/index.ts +++ b/api/src/paths/markdown/index.ts @@ -1,6 +1,5 @@ import { RequestHandler } from 'express'; import { Operation } from 'express-openapi'; -import { SYSTEM_ROLE } from '../../constants/roles'; import { getDBConnection } from '../../database/db'; import { markdownSchema } from '../../openapi/schemas/markdown'; import { authorizeRequestHandler } from '../../request-handlers/security/authorization'; @@ -14,8 +13,7 @@ export const GET: Operation = [ return { and: [ { - validSystemRoles: [SYSTEM_ROLE.PROJECT_CREATOR, SYSTEM_ROLE.SYSTEM_ADMIN, SYSTEM_ROLE.DATA_ADMINISTRATOR], - discriminator: 'SystemRole' + discriminator: 'SystemUser' } ] }; diff --git a/api/src/paths/markdown/{markdownId}/index.ts b/api/src/paths/markdown/{markdownId}/index.ts index 8115d713d1..c3e6d47734 100644 --- a/api/src/paths/markdown/{markdownId}/index.ts +++ b/api/src/paths/markdown/{markdownId}/index.ts @@ -1,6 +1,5 @@ import { RequestHandler } from 'express'; import { Operation } from 'express-openapi'; -import { SYSTEM_ROLE } from '../../../constants/roles'; import { getDBConnection } from '../../../database/db'; import { authorizeRequestHandler } from '../../../request-handlers/security/authorization'; import { MarkdownService } from '../../../services/markdown-service'; @@ -13,8 +12,7 @@ export const POST: Operation = [ return { and: [ { - validSystemRoles: [SYSTEM_ROLE.PROJECT_CREATOR, SYSTEM_ROLE.SYSTEM_ADMIN, SYSTEM_ROLE.DATA_ADMINISTRATOR], - discriminator: 'SystemRole' + discriminator: 'SystemUser' } ] }; @@ -37,7 +35,8 @@ POST.apiDoc = { description: 'Primary key of a markdown record to submit a score for', required: true, schema: { - type: 'integer' + type: 'integer', + minimum: 1 } } ], @@ -51,7 +50,11 @@ POST.apiDoc = { additionalProperties: false, required: ['score'], properties: { - score: { type: 'number', description: 'Score to add to the markdown record', enum: [-1, 1] } + score: { + type: 'number', + description: 'Score to add to the markdown record', + enum: [-1, 1] + } } } } diff --git a/api/src/repositories/markdown-repository.ts b/api/src/repositories/markdown-repository.ts index 568d157fd3..850fcef2e0 100644 --- a/api/src/repositories/markdown-repository.ts +++ b/api/src/repositories/markdown-repository.ts @@ -1,5 +1,6 @@ import SQL from 'sql-template-strings'; -import { MarkdownObject, MarkdownQueryObject, MarkdownUserObject } from '../models/markdown-view'; +import { MarkdownUserRecord } from '../database-models/markdown_user'; +import { MarkdownObject, MarkdownQueryObject } from '../models/markdown-view'; import { BaseRepository } from './base-repository'; /** @@ -66,10 +67,10 @@ export class MarkdownRepository extends BaseRepository { * * @param {number} markdownId * @param {number} systemUserId - * @return {*} {Promise} + * @return {*} {Promise} * @memberof MarkdownRepository */ - async getUserParticipation(markdownId: number, systemUserId: number): Promise { + async getUserParticipation(markdownId: number, systemUserId: number): Promise { const sqlStatement = SQL` SELECT markdown_user_id, @@ -83,7 +84,7 @@ export class MarkdownRepository extends BaseRepository { system_user_id = ${systemUserId}; `; - const response = await this.connection.sql(sqlStatement, MarkdownUserObject); + const response = await this.connection.sql(sqlStatement, MarkdownUserRecord); return response.rows?.[0] ?? null; } diff --git a/api/src/services/markdown-service.ts b/api/src/services/markdown-service.ts index 78edceadfe..a0a0654088 100644 --- a/api/src/services/markdown-service.ts +++ b/api/src/services/markdown-service.ts @@ -1,5 +1,6 @@ +import { MarkdownUserRecord } from '../database-models/markdown_user'; import { IDBConnection } from '../database/db'; -import { MarkdownObject, MarkdownQueryObject, MarkdownUserObject } from '../models/markdown-view'; +import { MarkdownObject, MarkdownQueryObject } from '../models/markdown-view'; import { MarkdownRepository } from '../repositories/markdown-repository'; import { DBService } from './db-service'; @@ -66,10 +67,10 @@ export class MarkdownService extends DBService { * * @param {number} markdownId * @param {number} systemUserId - * @return {*} Promise + * @return {*} Promise * @memberof MarkdownService */ - async getUserParticipation(markdownId: number, systemUserId: number): Promise { + async getUserParticipation(markdownId: number, systemUserId: number): Promise { return this.markdownRepository.getUserParticipation(markdownId, systemUserId); }