Skip to content

Commit

Permalink
fix: issues with share create endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
dahal committed Jul 21, 2024
1 parent f36b9fe commit 0038681
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 113 deletions.
18 changes: 6 additions & 12 deletions src/server/api/routes/company/share/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { withCompanyAuth } from "@/server/api/auth";
import { ApiError, ErrorResponses } from "@/server/api/error";
import type { PublicAPI } from "@/server/api/hono";
import { AddShareSchema } from "@/server/api/schema/shares";
import { CreateShareSchema } from "@/server/api/schema/shares";
import { getHonoUserAgent, getIp } from "@/server/api/utils";
import { addShare } from "@/server/services/shares/add-share";
import { createRoute, z } from "@hono/zod-openapi";
import type { Context } from "hono";
Expand All @@ -23,7 +24,7 @@ const ParamsSchema = z.object({

const ResponseSchema = z.object({
message: z.string(),
data: AddShareSchema,
data: CreateShareSchema,
});

const route = createRoute({
Expand All @@ -37,7 +38,7 @@ const route = createRoute({
body: {
content: {
"application/json": {
schema: AddShareSchema,
schema: CreateShareSchema,
},
},
},
Expand All @@ -58,21 +59,14 @@ const route = createRoute({
const create = (app: PublicAPI) => {
app.openapi(route, async (c: Context) => {
const { company, member, user } = await withCompanyAuth(c);

const body = await c.req.json();

const requestIP =
c.req.header("x-forwarded-for") ||
c.req.header("remoteAddr") ||
"Unknown IP";
const userAgent = c.req.header("User-Agent") || "";

const { success, message, data } = await addShare({
...body,
companyId: company.id,
memberId: member.id,
requestIP,
userAgent,
requestIP: getIp(c.req),
userAgent: getHonoUserAgent(c.req),
user: {
id: user.id,
name: user.name,
Expand Down
111 changes: 10 additions & 101 deletions src/server/api/schema/shares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ShareLegendsEnum,
VestingScheduleEnum,
} from "@prisma/client";
import { Share } from "next/font/google";

const VestingScheduleArr = Object.values(VestingScheduleEnum) as [
string,
Expand Down Expand Up @@ -127,106 +128,14 @@ export const ShareSchema = z
description: "Get a Single Share by the ID",
});

export const AddShareSchema = z
.object({
status: z.enum(SecuritiesStatusArr).openapi({
description: "Security Status",
example: "DRAFT",
}),

certificateId: z.string().openapi({
description: "Certificate ID",
example: "cly13ipa40000i7ng42mv4x7b",
}),

quantity: z.number().min(0).openapi({
description: "Quantity of Shares",
example: 234,
}),

pricePerShare: z.number().min(0).openapi({
description: "Price Per Share",
example: 23.4,
}),

capitalContribution: z.number().min(0).openapi({
description: "Total amount of money contributed",
example: 25.4,
}),

ipContribution: z.number().min(0).openapi({
description: "Value of the intellectual property contributed",
example: 43.4,
}),

debtCancelled: z.number().min(0).openapi({
description: "Amount of debt cancelled",
example: 54.54,
}),

otherContributions: z.number().min(0).openapi({
description: "Other contributions",
example: 45.54,
}),

vestingSchedule: z.enum(VestingScheduleArr).openapi({
description: "Vesting Schedule",
example: "VESTING_0_0_0",
}),

companyLegends: z
.enum(ShareLegendsArr)
.array()
.openapi({
description: "Company Legends",
example: ["US_SECURITIES_ACT", "SALE_AND_ROFR"],
}),

issueDate: z.string().datetime().openapi({
description: "Issued Date",
example: "2024-01-01T00:00:00.000Z",
}),

rule144Date: z.string().datetime().openapi({
description: "Rule 144 Date",
example: "2024-01-01T00:00:00.000Z",
}),

vestingStartDate: z.string().datetime().openapi({
description: "Vesting Start Date",
example: "2024-01-01T00:00:00.000Z",
}),

boardApprovalDate: z.string().datetime().openapi({
description: "Board Approval Date",
example: "2024-01-01T00:00:00.000Z",
}),

stakeholderId: z.string().cuid().openapi({
description: "StakeHolder ID",
example: "clydxglyl0000d94ff363egje",
}),

shareClassId: z.string().cuid().openapi({
description: "ShareClass ID",
example: "clycjzdqo000cjvsqr9rn6479",
}),

// documents: z
// .array(
// z.object({
// bucketId: z.string(),
// name: z.string(),
// }),
// )
// .openapi({
// description: "Uploaded Documents",
// example: [{ bucketId: "vbhjewhvjvj3", name: "Bucket Blue" }],
// }),
})
.openapi({
description: "Create a Share",
});
export const CreateShareSchema = ShareSchema.omit({
id: true,
createdAt: true,
updatedAt: true,
companyId: true,
}).openapi({
description: "Issue shares to a stakeholder in a company.",
});

export const UpdateShareSchema = ShareSchema.omit({
id: true,
Expand All @@ -247,5 +156,5 @@ export const UpdateShareSchema = ShareSchema.omit({
});

export type ShareSchemaType = z.infer<typeof ShareSchema>;
export type AddShareSchemaType = z.infer<typeof AddShareSchema>;
export type CreateShareSchemaType = z.infer<typeof CreateShareSchema>;
export type UpdateShareSchemaType = z.infer<typeof UpdateShareSchema>;

0 comments on commit 0038681

Please sign in to comment.