Skip to content

Commit

Permalink
fix: update types for created/updated and vote value (#45)
Browse files Browse the repository at this point in the history
* fix: update types for created/updated and vote value

* fix(drafts): make draft fields nullable and adjust response
  • Loading branch information
randomontherun authored May 31, 2024
1 parent 584dcad commit 074be21
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
10 changes: 5 additions & 5 deletions database/db_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SETUP VOTING TABLE
CREATE TABLE if NOT EXISTS votes (
voter_email TEXT NOT NULL,
proposal_id INT NOT NULL REFERENCES proposals(id),
vote varchar(32) NOT NULL,
vote INT,
comment TEXT DEFAULT '' NOT NULL ,
created TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated TIMESTAMPTZ,
Expand All @@ -50,10 +50,10 @@ EXECUTE PROCEDURE trigger_set_updated();
SETUP DRAFTS TABLE
*/
CREATE TABLE if NOT EXISTS drafts (
title varchar(100) DEFAULT '' NOT NULL,
summary TEXT DEFAULT '' NOT NULL,
description TEXT DEFAULT '' NOT NULL,
type varchar(32) DEFAULT '' NOT NULL,
title varchar(48),
summary TEXT,
description TEXT,
type varchar(32),
id SERIAL PRIMARY KEY,
created TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated TIMESTAMPTZ
Expand Down
9 changes: 7 additions & 2 deletions routes/drafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from "express";
import DraftsService from '../services/drafts';
import { SchemaValidationError } from 'slonik';
import { formatQueryErrorResponse, validateRequest } from '../helpers';
import { PendingDraft, DraftUpdate } from '../types/draft';
import {PendingDraft, DraftUpdate, Draft} from '../types/draft';
import { z } from "zod";

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

try{
const draft = await DraftsService.store(validationResult);
let draft = await DraftsService.store(validationResult);
draft = Object.fromEntries(
Object.entries(draft)
.filter(([_, v]) => v != null)
) as Draft

return res.status(201).json(draft);
}catch(e){
console.log(e)
Expand Down
2 changes: 1 addition & 1 deletion services/drafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function store(data: PendingDraft): Promise<Draft> {
return await pool.connect(async (connection) => {
const draft = await connection.one(sql.type(Draft)`
INSERT INTO drafts (title, summary, description, type)
VALUES (${data.title}, ${data.summary}, ${data.description}, ${data.type})
VALUES (${data.title ?? null}, ${data.summary ?? null}, ${data.description ?? null}, ${data.type ?? null})
RETURNING *;`)

return draft;
Expand Down
20 changes: 10 additions & 10 deletions types/draft.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { z } from 'zod'
const Draft = z.object({
title: z.string().max(48),
summary: z.string().max(255),
description: z.string().max(2048),
type: z.enum(['topic', 'project']),
title: z.string().max(48).optional(),
summary: z.string().max(255).optional(),
description: z.string().max(2048).optional(),
type: z.enum(['topic', 'project']).optional(),
id: z.number().int().positive(),
created: z.number().transform(val => new Date(val)),
updated: z.number().transform(val => new Date(val)).nullable(),
created: z.string(),
updated: z.string().nullable().optional(),
})

type Draft = z.infer<typeof Draft>

const PendingDraft = z.object({
title: z.string().max(48),
summary: z.string().max(255),
description: z.string().max(2048),
type: z.enum(['topic', 'project']),
title: z.string().max(48).optional().nullable(),
summary: z.string().max(255).optional().nullable(),
description: z.string().max(2048).optional().nullable(),
type: z.enum(['topic', 'project']).optional().nullable(),
})

type PendingDraft = z.infer<typeof PendingDraft>
Expand Down
4 changes: 2 additions & 2 deletions types/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { z } from 'zod'

const Proposal = z.object({
id: z.number().int().positive(),
created: z.number().transform(val => new Date(val)),
updated: z.number().transform(val => new Date(val)).nullable(),
created: z.string(),
updated: z.string().nullable(),
title: z.string().min(8).max(48),
summary: z.string().min(30).max(255),
description: z.string().max(2048),
Expand Down
8 changes: 4 additions & 4 deletions types/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {PendingDraft} from "./draft";
const Vote = z.object({
email: z.string().max(255),
proposalId: z.number().int().positive(),
value: z.enum(["-2", "-1", "0", "1", "2"]),
value: z.number().min(-2).max(2).nullable(),
comment: z.string().max(255),
id: z.number().int().positive(),
created: z.number().transform(val => new Date(val)),
updated: z.number().transform(val => new Date(val)).nullable(),
created: z.string(),
updated: z.string().nullable(),
})

type Vote = z.infer<typeof Vote>

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

Expand Down

0 comments on commit 074be21

Please sign in to comment.