Skip to content

Commit

Permalink
chore: unknown type for hono response
Browse files Browse the repository at this point in the history
  • Loading branch information
Raju-kadel-27 committed Jul 29, 2024
1 parent c544f18 commit a5158db
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
17 changes: 9 additions & 8 deletions src/server/api/routes/company/stock-option/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
ErrorResponses,
} from "@/server/api/error";
import type { PublicAPI } from "@/server/api/hono";
import { OptionSchema, type TCreateOptionSchema } from "../../../schema/option";
import type { TOptionSchema } from "./../../../schema/option";

import { CreateOptionSchema } from "@/server/api/schema/option";
import {
OptionSchema,
type TCreateOptionSchema,
} from "@/server/api/schema/option";
import { getHonoUserAgent, getIp } from "@/server/api/utils";
import { addOption } from "@/server/services/stock-option/add-option";
import { createRoute, z } from "@hono/zod-openapi";
Expand Down Expand Up @@ -37,7 +38,7 @@ const ResponseSchema = z.object({
const route = createRoute({
method: "post",
path: "/v1/companies/{id}/options",
summary: "Issue option",
summary: "Issue a new stock option",
description: "Issue stock option to a stakeholder in a company.",
tags: ["Options"],
request: {
Expand Down Expand Up @@ -69,7 +70,7 @@ const create = (app: PublicAPI) => {
const body = await c.req.json();

const { success, message, data, code } = await addOption({
requestIP: getIp(c.req),
requestIp: getIp(c.req),
userAgent: getHonoUserAgent(c.req),
data: body as TCreateOptionSchema,
companyId: company.id,
Expand All @@ -80,7 +81,7 @@ const create = (app: PublicAPI) => {
},
});

if (!success) {
if (!success || !data) {
throw new ApiError({
code: code as ErrorCodeType,
message,
Expand All @@ -89,8 +90,8 @@ const create = (app: PublicAPI) => {

return c.json(
{
message: "Stock option added successfully.",
data: data as unknown as TOptionSchema,
message,
data,
},
200,
);
Expand Down
4 changes: 2 additions & 2 deletions src/server/api/routes/company/stock-option/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const ResponseSchema = z
const route = createRoute({
method: "delete",
path: "/v1/companies/{id}/options/{optionId}",
summary: "Delete issued options",
description: "Delete a Option by ID",
summary: "Delete an option by ID",
description: "Delete an Option by ID",
tags: ["Options"],
request: {
params: RequestParamsSchema,
Expand Down
7 changes: 3 additions & 4 deletions src/server/api/routes/company/stock-option/getOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,19 @@ const getOne = (app: PublicAPI) => {
app.openapi(route, async (c: Context) => {
const { company } = await withCompanyAuth(c);

// id destructured to companyId and optionId destructured to id
const { optionId: id } = c.req.param();

const option = (await db.option.findUnique({
const option = await db.option.findUnique({
where: {
id,
companyId: company.id,
},
})) as unknown as TOptionSchema;
});

if (!option) {
throw new ApiError({
code: "NOT_FOUND",
message: "Option not found",
message: "Required option not found",
});
}

Expand Down
19 changes: 9 additions & 10 deletions src/server/api/routes/company/stock-option/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { PublicAPI } from "@/server/api/hono";

import {
OptionSchema,
type TOptionSchema,
type TUpdateOptionSchema,
UpdateOptionSchema,
} from "@/server/api/schema/option";
Expand Down Expand Up @@ -45,7 +44,7 @@ export const RequestParamsSchema = z
}),
})
.openapi({
description: "Update a Option by ID",
description: "Update an option by ID",
});

const ResponseSchema = z
Expand All @@ -54,14 +53,14 @@ const ResponseSchema = z
data: OptionSchema,
})
.openapi({
description: "Update a Option by ID",
description: "Update an option by ID",
});

const route = createRoute({
method: "put",
path: "/v1/companies/{id}/options/{optionId}",
summary: "Update issued options by ID",
description: "Update issued options by option ID",
summary: "Update an issued option by ID",
description: "Update issued option by option ID",
tags: ["Options"],
request: {
params: RequestParamsSchema,
Expand All @@ -86,7 +85,7 @@ const route = createRoute({
},
});

const getOne = (app: PublicAPI) => {
const update = (app: PublicAPI) => {
app.openapi(route, async (c: Context) => {
const { company, user } = await withCompanyAuth(c);
const { optionId } = c.req.param();
Expand All @@ -106,7 +105,7 @@ const getOne = (app: PublicAPI) => {

const { success, message, code, data } = await updateOption(payload);

if (!success) {
if (!success || !data) {
throw new ApiError({
code: code as ErrorCodeType,
message,
Expand All @@ -115,12 +114,12 @@ const getOne = (app: PublicAPI) => {

return c.json(
{
message: message,
data: data as unknown as TOptionSchema,
message,
data,
},
200,
);
});
};

export default getOne;
export default update;

0 comments on commit a5158db

Please sign in to comment.