Skip to content

Commit b54295c

Browse files
authored
uth-239: Comments database schema + API/routes
1 parent 1c58a84 commit b54295c

File tree

7 files changed

+361
-5
lines changed

7 files changed

+361
-5
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param { import("knex").Knex } knex
3+
* @returns { Promise<void> }
4+
*/
5+
exports.up = function(knex) {
6+
return knex.transaction(async (trx) => {
7+
8+
await trx.raw(`
9+
CREATE TABLE comment (
10+
id int NOT NULL PRIMARY KEY IDENTITY(1,1),
11+
targetType nvarchar(30) NOT NULL,
12+
targetId int NOT NULL,
13+
authorId nvarchar(100) NOT NULL,
14+
authorName nvarchar(50) NOT NULL,
15+
type nvarchar(8) NOT NULL CHECK (type IN ('COMMENT', 'WARNING', 'STOP')),
16+
comment nvarchar(max) NOT NULL,
17+
createdAt datetimeoffset NOT NULL DEFAULT SYSDATETIMEOFFSET()
18+
);
19+
20+
CREATE INDEX idx_thread_id
21+
ON comment (targetType, targetId);
22+
`)
23+
})
24+
};
25+
26+
/**
27+
* @param { import("knex").Knex } knex
28+
* @returns { Promise<void> }
29+
*/
30+
exports.down = function(knex) {
31+
return knex.transaction(async (trx) => {
32+
33+
await trx.raw(`
34+
DROP TABLE comment;
35+
`)
36+
})
37+
};

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"koa-pino-logger": "^4.0.0",
7575
"koa2-swagger-ui": "^5.10.0",
7676
"mssql": "^11.0.1",
77-
"onecore-types": "^3.7.1",
77+
"onecore-types": "^3.11.0",
7878
"onecore-utilities": "^1.1.0",
7979
"personnummer": "^3.2.1",
8080
"pino": "^9.1.0",
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { Comment, CommentThread, CommentThreadId } from 'onecore-types'
2+
import { DbComment } from './types'
3+
import { leasing } from 'onecore-types'
4+
import z from 'zod'
5+
6+
import { db } from './db'
7+
8+
const getCommentThreadById = async (
9+
threadId: CommentThreadId,
10+
dbConnection = db
11+
): Promise<CommentThread | undefined> => {
12+
const comments = await dbConnection
13+
.from('comment AS c')
14+
.select<Array<DbComment>>(
15+
'c.id AS Id',
16+
'c.createdAt AS CreatedAt',
17+
'c.type AS Type',
18+
'c.authorId AS AuthorId',
19+
'c.authorName AS AuthorName',
20+
'c.comment AS Comment'
21+
)
22+
.where({
23+
TargetType: threadId.targetType,
24+
TargetId: threadId.targetId,
25+
})
26+
.orderBy('CreatedAt', 'desc')
27+
28+
return {
29+
id: threadId,
30+
comments: comments.map((c: DbComment) => {
31+
return {
32+
id: c.Id,
33+
authorName: c.AuthorName,
34+
authorId: c.AuthorId,
35+
type: c.Type,
36+
comment: c.Comment,
37+
createdAt: c.CreatedAt,
38+
}
39+
}),
40+
}
41+
}
42+
43+
type AddCommentRequest = z.infer<
44+
typeof leasing.v1.AddCommentRequestParamsSchema
45+
>
46+
47+
const addComment = async (
48+
threadId: CommentThreadId,
49+
comment: AddCommentRequest,
50+
dbConnection = db
51+
): Promise<Comment | undefined> => {
52+
const [inserted] = await dbConnection
53+
.table('comment')
54+
.insert({
55+
TargetType: threadId.targetType,
56+
TargetId: threadId.targetId,
57+
AuthorName: comment.authorName,
58+
AuthorId: comment.authorId,
59+
Type: comment.type,
60+
Comment: comment.comment,
61+
})
62+
.returning('*')
63+
64+
return {
65+
id: inserted.id,
66+
authorName: inserted.authorName,
67+
authorId: inserted.authorId,
68+
type: inserted.type,
69+
comment: inserted.comment,
70+
createdAt: inserted.createdAt,
71+
}
72+
}
73+
74+
const removeComment = async (
75+
threadId: CommentThreadId,
76+
commentId: number,
77+
dbConnection = db
78+
): Promise<void | undefined> => {
79+
await dbConnection
80+
.table('comment')
81+
.where({
82+
Id: commentId,
83+
TargetType: threadId.targetType,
84+
TargetId: threadId.targetId,
85+
})
86+
.delete()
87+
88+
return
89+
}
90+
91+
export default { getCommentThreadById, addComment, removeComment }

src/services/lease-service/adapters/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,15 @@ export type DbListing = {
7878
WaitingListType: string | null
7979
}
8080

81+
export type DbComment = {
82+
Id: number
83+
TargetType: string
84+
TargetId: number
85+
AuthorName: string
86+
AuthorId: string
87+
CreatedAt: Date
88+
Type: 'COMMENT' | 'WARNING' | 'STOP'
89+
Comment: string
90+
}
91+
8192
export type AdapterResult<T, E> = { ok: true; data: T } | { ok: false; err: E }

src/services/lease-service/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import KoaRouter from '@koa/router'
22
import { routes as offerRoutes } from './routes/offers'
3+
import { routes as commentRoutes } from './routes/comments'
34
import { routes as contactRoutes } from './routes/contacts'
45
import { routes as invoiceRoutes } from './routes/invoices'
56
import { routes as leaseRoutes } from './routes/leases'
@@ -9,6 +10,7 @@ import { routes as applicantsRoutes } from './routes/applicants'
910
export const routes = (router: KoaRouter) => {
1011
applicantsRoutes(router)
1112
offerRoutes(router)
13+
commentRoutes(router)
1214
contactRoutes(router)
1315
invoiceRoutes(router)
1416
leaseRoutes(router)

0 commit comments

Comments
 (0)