-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathadd-share.ts
More file actions
85 lines (77 loc) · 2.62 KB
/
Copy pathadd-share.ts
File metadata and controls
85 lines (77 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { generatePublicId } from "@/common/id";
import { Audit } from "@/server/audit";
import { checkMembership } from "@/server/auth";
import { withAuth } from "@/trpc/api/trpc";
import { ZodAddShareMutationSchema } from "../schema";
export const addShareProcedure = withAuth
.input(ZodAddShareMutationSchema)
.mutation(async ({ ctx, input }) => {
const { userAgent, requestIp } = ctx;
try {
const user = ctx.session.user;
const documents = input.documents;
await ctx.db.$transaction(async (tx) => {
const { companyId } = await checkMembership({
session: ctx.session,
tx,
});
const data = {
companyId,
stakeholderId: input.stakeholderId,
shareClassId: input.shareClassId,
status: input.status,
certificateId: input.certificateId,
quantity: input.quantity,
pricePerShare: input.pricePerShare,
capitalContribution: input.capitalContribution,
ipContribution: input.ipContribution,
debtCancelled: input.debtCancelled,
otherContributions: input.otherContributions,
cliffMonths: input.cliffMonths,
vestingMonths: input.vestingMonths,
companyLegends: input.companyLegends,
issueDate: new Date(input.issueDate),
rule144Date: new Date(input.rule144Date),
vestingStartDate: new Date(input.vestingStartDate),
boardApprovalDate: new Date(input.boardApprovalDate),
};
const share = await tx.share.create({ data });
const bulkDocuments = documents.map((doc) => ({
companyId,
uploaderId: user.memberId,
publicId: generatePublicId(),
name: doc.name,
bucketId: doc.bucketId,
shareId: share.id,
}));
await tx.document.createMany({
data: bulkDocuments,
skipDuplicates: true,
});
await Audit.create(
{
action: "share.created",
companyId: user.companyId,
actor: { type: "user", id: user.id },
context: {
userAgent,
requestIp,
},
target: [{ type: "share", id: share.id }],
summary: `${user.name} added share for stakeholder ${input.stakeholderId}`,
},
tx,
);
});
return {
success: true,
message: "🎉 Successfully added a share",
};
} catch (error) {
console.error("Error adding shares: ", error);
return {
success: false,
message: "Please use unique Certificate Id.",
};
}
});