Skip to content

Commit

Permalink
fixed typescript bugs and refactored code to dry it out
Browse files Browse the repository at this point in the history
  • Loading branch information
kurikurichan committed Apr 24, 2024
1 parent fe63f65 commit d749a91
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 75 deletions.
3 changes: 2 additions & 1 deletion server/app/controllers/suggestion-controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import suggestionService from "../services/suggestion-service";
import { RequestHandler, Response } from "express";
import { Suggestion } from "../../types/suggestion-types";
import { SuggestionPostFields } from "../validation-schema/suggestion-schema";

const getAll: RequestHandler<
// route params
Expand Down Expand Up @@ -42,7 +43,7 @@ const getById: RequestHandler<{ id: string }, Suggestion, never> = async (
const post: RequestHandler<
never,
{ id: number } | { error: string },
Suggestion
SuggestionPostFields
> = async (req, res) => {
try {
const resp = await suggestionService.insert(req.body);
Expand Down
6 changes: 3 additions & 3 deletions server/app/services/suggestion-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import db from "./db";
import camelcaseKeys from "camelcase-keys";
import { Suggestion } from "../../types/suggestion-types";
import { SuggestionPostFields } from "../validation-schema/suggestion-schema";

const selectAll = async (params: {
statusIds: string[];
Expand Down Expand Up @@ -43,8 +44,7 @@ const selectById = async (suggestionId: string): Promise<Suggestion> => {
return camelcaseKeys(row);
};

const insert = async (model: Suggestion): Promise<{ id: number }> => {
model.suggestionStatusId = 1;
const insert = async (model: SuggestionPostFields): Promise<{ id: number }> => {
const sql = `insert into suggestion (
name, address_1, address_2,
city, state, zip,
Expand All @@ -59,7 +59,7 @@ const insert = async (model: Suggestion): Promise<{ id: number }> => {
$<hours>, $<category>, $<tenantId>, $<stakeholderId>
)
returning id`;
const result = await db.one(sql, model);
const result = await db.one(sql, { ...model, suggestionStatusId: 1 });
return { id: result.id };
};

Expand Down
129 changes: 62 additions & 67 deletions server/app/validation-schema/suggestion-schema.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,68 @@
import { JSONSchemaType } from "ajv";
import { Suggestion } from "../../types/suggestion-types";

export const suggestionPostRequestSchema: JSONSchemaType<
Omit<Suggestion, "id">
> = {
type: "object",
required: ["name", "tenantId"],
properties: {
stakeholderId: {
type: "integer",
nullable: true,
anyOf: [{ minimum: 1 }, { type: ["integer", "null"] }],
},
adminNotes: {
type: "string",
},
suggestionStatusId: { type: ["integer", "null"], nullable: true },
name: {
type: ["string", "null"],
minLength: 1,
},
address1: {
type: "string",
},
address2: {
type: "string",
},
city: {
type: "string",
},
state: {
type: "string",
},
zip: {
type: "string",
},
phone: {
type: "string",
},
email: {
// once we have set a value to format/pattern property, that property becomes required
// since email is optional, we can make UnionType to accept empty strings
// setting minLength to 0 or setting nullable: true won't help to leave email empty
type: "string",
anyOf: [
{ format: "email" },
{ maxLength: 0 }, // for an empty string
],
},
notes: { type: "string" }, // also called "other information"
tipsterName: {
type: "string",
},
tipsterPhone: {
type: "string",
},
tipsterEmail: {
type: "string",
anyOf: [{ format: "email" }, { maxLength: 0 }],
},
hours: { type: "string" },
category: { type: "string" },
tenantId: {
type: "integer",
minimum: 1,
},
},
additionalProperties: true,
};
export type SuggestionPostFields = Omit<
Suggestion,
"id" | "suggestionStatusId" | "stakeholderId" | "adminNotes"
>;

export const suggestionPostRequestSchema: JSONSchemaType<SuggestionPostFields> =
{
type: "object",
required: ["name", "tenantId"],
properties: {
name: {
type: "string",
minLength: 1,
},
address1: {
type: "string",
},
address2: {
type: "string",
},
city: {
type: "string",
},
state: {
type: "string",
},
zip: {
type: "string",
},
phone: {
type: "string",
},
email: {
// once we have set a value to format/pattern property, that property becomes required
// since email is optional, we can make UnionType to accept empty strings
// setting minLength to 0 or setting nullable: true won't help to leave email empty
type: "string",
anyOf: [
{ format: "email" },
{ maxLength: 0 }, // for an empty string
],
},
notes: { type: "string" }, // also called "other information"
tipsterName: {
type: "string",
},
tipsterPhone: {
type: "string",
},
tipsterEmail: {
type: "string",
anyOf: [{ format: "email" }, { maxLength: 0 }],
},
hours: { type: "string" },
category: { type: "string" },
tenantId: {
type: "integer",
minimum: 1,
},
},
additionalProperties: true,
};

export const suggestionPutRequestSchema: JSONSchemaType<Suggestion> = {
type: "object",
Expand Down
8 changes: 4 additions & 4 deletions server/types/suggestion-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface Suggestion {
id: number | null;
name: string | null;
id: number;
name: string;
address1: string;
address2: string;
city: string;
Expand All @@ -15,7 +15,7 @@ export interface Suggestion {
hours: string;
category: string;
suggestionStatusId: number;
adminNotes?: string;
stakeholderId: number | null;
adminNotes: string;
stakeholderId: number;
tenantId: number;
}

0 comments on commit d749a91

Please sign in to comment.