Skip to content

Commit 074be21

Browse files
fix: update types for created/updated and vote value (#45)
* fix: update types for created/updated and vote value * fix(drafts): make draft fields nullable and adjust response
1 parent 584dcad commit 074be21

6 files changed

Lines changed: 29 additions & 24 deletions

File tree

database/db_init.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SETUP VOTING TABLE
3333
CREATE TABLE if NOT EXISTS votes (
3434
voter_email TEXT NOT NULL,
3535
proposal_id INT NOT NULL REFERENCES proposals(id),
36-
vote varchar(32) NOT NULL,
36+
vote INT,
3737
comment TEXT DEFAULT '' NOT NULL ,
3838
created TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
3939
updated TIMESTAMPTZ,
@@ -50,10 +50,10 @@ EXECUTE PROCEDURE trigger_set_updated();
5050
SETUP DRAFTS TABLE
5151
*/
5252
CREATE TABLE if NOT EXISTS drafts (
53-
title varchar(100) DEFAULT '' NOT NULL,
54-
summary TEXT DEFAULT '' NOT NULL,
55-
description TEXT DEFAULT '' NOT NULL,
56-
type varchar(32) DEFAULT '' NOT NULL,
53+
title varchar(48),
54+
summary TEXT,
55+
description TEXT,
56+
type varchar(32),
5757
id SERIAL PRIMARY KEY,
5858
created TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
5959
updated TIMESTAMPTZ

routes/drafts.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from "express";
22
import DraftsService from '../services/drafts';
33
import { SchemaValidationError } from 'slonik';
44
import { formatQueryErrorResponse, validateRequest } from '../helpers';
5-
import { PendingDraft, DraftUpdate } from '../types/draft';
5+
import {PendingDraft, DraftUpdate, Draft} from '../types/draft';
66
import { z } from "zod";
77

88
const router = express.Router();
@@ -81,7 +81,12 @@ router.post("/", validateRequest(PostRequest), async (req, res) => {
8181
const validationResult = req.validated.body as PendingDraft
8282

8383
try{
84-
const draft = await DraftsService.store(validationResult);
84+
let draft = await DraftsService.store(validationResult);
85+
draft = Object.fromEntries(
86+
Object.entries(draft)
87+
.filter(([_, v]) => v != null)
88+
) as Draft
89+
8590
return res.status(201).json(draft);
8691
}catch(e){
8792
console.log(e)

services/drafts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function store(data: PendingDraft): Promise<Draft> {
2121
return await pool.connect(async (connection) => {
2222
const draft = await connection.one(sql.type(Draft)`
2323
INSERT INTO drafts (title, summary, description, type)
24-
VALUES (${data.title}, ${data.summary}, ${data.description}, ${data.type})
24+
VALUES (${data.title ?? null}, ${data.summary ?? null}, ${data.description ?? null}, ${data.type ?? null})
2525
RETURNING *;`)
2626

2727
return draft;

types/draft.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { z } from 'zod'
22
const Draft = z.object({
3-
title: z.string().max(48),
4-
summary: z.string().max(255),
5-
description: z.string().max(2048),
6-
type: z.enum(['topic', 'project']),
3+
title: z.string().max(48).optional(),
4+
summary: z.string().max(255).optional(),
5+
description: z.string().max(2048).optional(),
6+
type: z.enum(['topic', 'project']).optional(),
77
id: z.number().int().positive(),
8-
created: z.number().transform(val => new Date(val)),
9-
updated: z.number().transform(val => new Date(val)).nullable(),
8+
created: z.string(),
9+
updated: z.string().nullable().optional(),
1010
})
1111

1212
type Draft = z.infer<typeof Draft>
1313

1414
const PendingDraft = z.object({
15-
title: z.string().max(48),
16-
summary: z.string().max(255),
17-
description: z.string().max(2048),
18-
type: z.enum(['topic', 'project']),
15+
title: z.string().max(48).optional().nullable(),
16+
summary: z.string().max(255).optional().nullable(),
17+
description: z.string().max(2048).optional().nullable(),
18+
type: z.enum(['topic', 'project']).optional().nullable(),
1919
})
2020

2121
type PendingDraft = z.infer<typeof PendingDraft>

types/proposal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { z } from 'zod'
22

33
const Proposal = z.object({
44
id: z.number().int().positive(),
5-
created: z.number().transform(val => new Date(val)),
6-
updated: z.number().transform(val => new Date(val)).nullable(),
5+
created: z.string(),
6+
updated: z.string().nullable(),
77
title: z.string().min(8).max(48),
88
summary: z.string().min(30).max(255),
99
description: z.string().max(2048),

types/vote.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import {PendingDraft} from "./draft";
33
const Vote = z.object({
44
email: z.string().max(255),
55
proposalId: z.number().int().positive(),
6-
value: z.enum(["-2", "-1", "0", "1", "2"]),
6+
value: z.number().min(-2).max(2).nullable(),
77
comment: z.string().max(255),
88
id: z.number().int().positive(),
9-
created: z.number().transform(val => new Date(val)),
10-
updated: z.number().transform(val => new Date(val)).nullable(),
9+
created: z.string(),
10+
updated: z.string().nullable(),
1111
})
1212

1313
type Vote = z.infer<typeof Vote>
1414

1515
const PendingVote = z.object({
16-
value: z.enum(["-2", "-1", "0", "1", "2"]),
16+
value: z.number().min(-2).max(2).nullable(),
1717
comment: z.string().max(255)
1818
})
1919

0 commit comments

Comments
 (0)