1
1
import { generatePublicId } from "@/common/id" ;
2
- import {
3
- type TCreateOptionSchema ,
4
- TOptionSchema ,
5
- } from "@/server/api/schema/option" ;
2
+ import type { TCreateOptionSchema } from "@/server/api/schema/option" ;
6
3
import { Audit } from "@/server/audit" ;
7
4
import { db } from "@/server/db" ;
8
5
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library" ;
6
+ import type { TUpdateOption } from "./update-option" ;
9
7
10
- interface TAddOption {
11
- companyId : string ;
12
- requestIP : string ;
13
- userAgent : string ;
8
+ type AuditPromise = ReturnType < typeof Audit . create > ;
9
+
10
+ type TAddOption = Omit < TUpdateOption , "data" | "optionId" > & {
14
11
data : TCreateOptionSchema ;
15
12
memberId : string ;
16
- user : {
17
- id : string ;
18
- name : string ;
19
- } ;
20
- }
13
+ } ;
21
14
22
15
export const addOption = async ( payload : TAddOption ) => {
23
16
try {
24
17
const { data, user, memberId } = payload ;
25
18
const documents = data . documents ;
26
19
27
- const issuedOption = await db . $transaction ( async ( tx ) => {
20
+ const newOption = await db . $transaction ( async ( tx ) => {
28
21
const _data = {
29
22
grantId : data . grantId ,
30
23
quantity : data . quantity ,
@@ -46,8 +39,7 @@ export const addOption = async (payload: TAddOption) => {
46
39
47
40
const option = await tx . option . create ( { data : _data } ) ;
48
41
49
- // biome-ignore lint/suspicious/noExplicitAny: <explain>
50
- let auditPromises : any = [ ] ;
42
+ let auditPromises : AuditPromise [ ] = [ ] ;
51
43
52
44
if ( documents && documents . length > 0 ) {
53
45
const bulkDocuments = documents . map ( ( doc ) => ( {
@@ -72,53 +64,48 @@ export const addOption = async (payload: TAddOption) => {
72
64
actor : { type : "user" , id : user . id } ,
73
65
context : {
74
66
userAgent : payload . userAgent ,
75
- requestIp : payload . requestIP ,
67
+ requestIp : payload . requestIp ,
76
68
} ,
77
69
target : [ { type : "document" , id : doc . id } ] ,
78
- summary : `${ user . name } created a document : ${ doc . name } ` ,
70
+ summary : `${ user . name } created a document while issuing a stock option : ${ doc . name } ` ,
79
71
} ,
80
72
tx ,
81
73
) ,
82
74
) ;
83
75
}
84
76
85
- await Promise . all ( [
86
- ...auditPromises ,
77
+ auditPromises . push (
87
78
Audit . create (
88
79
{
89
80
action : "option.created" ,
90
81
companyId : payload . companyId ,
91
82
actor : { type : "user" , id : user . id } ,
92
83
context : {
93
84
userAgent : payload . userAgent ,
94
- requestIp : payload . requestIP ,
85
+ requestIp : payload . requestIp ,
95
86
} ,
96
87
target : [ { type : "option" , id : option . id } ] ,
97
88
summary : `${ user . name } issued an option for stakeholder : ${ option . stakeholderId } ` ,
98
89
} ,
99
90
tx ,
100
91
) ,
101
- ] ) ;
92
+ ) ;
93
+
94
+ await Promise . all ( auditPromises ) ;
102
95
103
96
return option ;
104
97
} ) ;
105
98
106
99
return {
107
100
success : true ,
108
101
message : "🎉 Successfully issued an option." ,
109
- data : {
110
- ...issuedOption ,
111
- issueDate : issuedOption . issueDate . toISOString ( ) ,
112
- rule144Date : issuedOption . rule144Date . toISOString ( ) ,
113
- vestingStartDate : issuedOption . vestingStartDate . toISOString ( ) ,
114
- boardApprovalDate : issuedOption . boardApprovalDate . toISOString ( ) ,
115
- expirationDate : issuedOption . expirationDate . toISOString ( ) ,
116
- } ,
102
+ data : newOption ,
117
103
} ;
118
104
} catch ( error ) {
119
105
console . error ( error ) ;
120
106
if ( error instanceof PrismaClientKnownRequestError ) {
121
107
// Unique constraints error code in prisma
108
+ // Only grantId column can throw this error code
122
109
if ( error . code === "P2002" ) {
123
110
return {
124
111
success : false ,
0 commit comments