Skip to content

Commit 03abf4b

Browse files
committed
wip restore issues factory
1 parent acb114e commit 03abf4b

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

packages/core/src/services/issues/create.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ export async function createIssue(
1515
document,
1616
project,
1717
workspace,
18+
createdAt: createdAtArg,
1819
}: {
1920
title: string
2021
description: string
2122
document: DocumentVersion
2223
project: Project
2324
workspace: Workspace
25+
createdAt?: Date
2426
},
2527
transaction = new Transaction(),
2628
) {
27-
const createdAt = new Date()
28-
29+
const createdAt = createdAtArg ?? new Date()
2930
const centroid = createCentroid()
3031

3132
// Note: not creating the vector in the vector db yet to avoid storing empty vectors,
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { format } from 'date-fns'
2+
import { faker } from '@faker-js/faker'
3+
import { type Workspace } from '../../schema/models/types/Workspace'
4+
import { type DocumentVersion } from '../../schema/models/types/DocumentVersion'
5+
import { type Project } from '../../schema/models/types/Project'
6+
import { createIssue as createIssueService } from '../../services/issues/create'
7+
import { createWorkspace, type ICreateWorkspace } from './workspaces'
8+
import { createProject, type ICreateProject } from './projects'
9+
import { IssueHistogram } from '../../schema/models/types/IssueHistogram'
10+
import { type Issue } from '../../schema/models/types/Issue'
11+
import { Result } from '../../lib/Result'
12+
import Transaction from '../../lib/Transaction'
13+
import { issueHistograms } from '../../schema/models/issueHistograms'
14+
15+
export type IssueHistogramData = {
16+
issue: Issue
17+
commitId: number
18+
date: Date
19+
count: number
20+
}
21+
22+
async function createIssueHistogramsBulk(
23+
{
24+
workspace,
25+
histograms,
26+
}: {
27+
workspace: Workspace
28+
histograms: IssueHistogramData[]
29+
},
30+
transaction = new Transaction(),
31+
) {
32+
return transaction.call(async (tx) => {
33+
const values = histograms.map(({ issue, commitId, date, count }) => ({
34+
workspaceId: workspace.id,
35+
projectId: issue.projectId,
36+
documentUuid: issue.documentUuid,
37+
issueId: issue.id,
38+
commitId,
39+
date: format(date, 'yyyy-MM-dd'),
40+
count,
41+
}))
42+
43+
const results = await tx.insert(issueHistograms).values(values).returning()
44+
45+
return Result.ok(results)
46+
})
47+
}
48+
49+
export type ICreateIssue = {
50+
createdAt: Date
51+
document: DocumentVersion
52+
workspace?: Workspace | ICreateWorkspace
53+
project?: Project | ICreateProject
54+
title?: string
55+
description?: string
56+
histograms?: IssueHistogramData[]
57+
}
58+
59+
export async function createIssue(issueData: Partial<ICreateIssue> = {}) {
60+
if (!issueData.document) {
61+
throw new Error('DocumentVersion is required to create an Issue')
62+
}
63+
64+
const workspaceData = issueData.workspace ?? {}
65+
let workspace: Workspace
66+
let project: Project
67+
68+
if ('id' in workspaceData) {
69+
workspace = workspaceData as Workspace
70+
} else {
71+
const newWorkspace = await createWorkspace(workspaceData)
72+
workspace = newWorkspace.workspace
73+
}
74+
75+
const projectData = issueData.project ?? {}
76+
if ('id' in projectData) {
77+
project = projectData as Project
78+
} else {
79+
const newProject = await createProject({ workspace, ...projectData })
80+
project = newProject.project
81+
}
82+
83+
const title = issueData.title ?? faker.lorem.sentence()
84+
const description = issueData.description ?? faker.lorem.paragraph()
85+
86+
const result = await createIssueService({
87+
workspace,
88+
project,
89+
document: issueData.document,
90+
title,
91+
description,
92+
createdAt: issueData.createdAt,
93+
})
94+
95+
const issue = result.unwrap().issue
96+
let histograms: IssueHistogram[] = []
97+
98+
// Create histograms if provided
99+
if (issueData.histograms && issueData.histograms.length > 0) {
100+
const histogramResult = await createIssueHistogramsBulk({
101+
workspace,
102+
histograms: issueData.histograms.map((histogram) => ({
103+
...histogram,
104+
issue,
105+
})),
106+
})
107+
108+
if (histogramResult.error) {
109+
throw histogramResult.error
110+
}
111+
histograms = histogramResult.value
112+
}
113+
114+
return {
115+
issue,
116+
workspace,
117+
project,
118+
histograms,
119+
}
120+
}

0 commit comments

Comments
 (0)