Skip to content

Commit 5f8548f

Browse files
Add indexes to new project files (#234)
* Order files by index * Add index to new project files * Add index to new project files * Add index to new project files
1 parent 4ea8fce commit 5f8548f

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

controller/projects.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,35 @@ func (p *Projects) Create(user *model.User, input model.NewProject) (*model.Proj
8181

8282
files := make([]*model.File, 0)
8383

84-
for _, tpl := range input.ContractTemplates {
84+
for i, tpl := range input.ContractTemplates {
8585
files = append(files, &model.File{
8686
ID: uuid.New(),
8787
ProjectID: proj.ID,
8888
Title: tpl.Title,
8989
Script: tpl.Script,
90+
Index: i,
9091
Type: model.ContractFile,
9192
})
9293
}
9394

94-
for _, tpl := range input.TransactionTemplates {
95+
for i, tpl := range input.TransactionTemplates {
9596
files = append(files, &model.File{
9697
ID: uuid.New(),
9798
ProjectID: proj.ID,
9899
Title: tpl.Title,
99100
Script: tpl.Script,
101+
Index: i,
100102
Type: model.TransactionFile,
101103
})
102104
}
103105

104-
for _, tpl := range input.ScriptTemplates {
106+
for i, tpl := range input.ScriptTemplates {
105107
files = append(files, &model.File{
106108
ID: uuid.New(),
107109
ProjectID: proj.ID,
108110
Title: tpl.Title,
109111
Script: tpl.Script,
112+
Index: i,
110113
Type: model.ScriptFile,
111114
})
112115
}

e2eTest/constants.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ query($projectId: UUID!) {
189189
id
190190
contractTemplates {
191191
id
192+
title
192193
script
193194
index
194195
}
@@ -201,6 +202,7 @@ type GetProjectContractTemplatesResponse struct {
201202
ID string
202203
ContractTemplates []struct {
203204
ID string
205+
Title string
204206
Script string
205207
Index int
206208
}
@@ -343,6 +345,17 @@ mutation($templateId: UUID!, $projectId: UUID!, $script: String!) {
343345
}
344346
`
345347

348+
const MutationUpdateContractTemplateTitle = `
349+
mutation($templateId: UUID!, $projectId: UUID!, $title: String) {
350+
updateContractTemplate(input: { id: $templateId, projectId: $projectId, title: $title }) {
351+
id
352+
title
353+
script
354+
index
355+
}
356+
}
357+
`
358+
346359
const MutationUpdateContractTemplateIndex = `
347360
mutation($templateId: UUID!, $projectId: UUID!, $index: Int!) {
348361
updateContractTemplate(input: { id: $templateId, projectId: $projectId, index: $index }) {
@@ -356,8 +369,9 @@ mutation($templateId: UUID!, $projectId: UUID!, $index: Int!) {
356369
type UpdateContractTemplateResponse struct {
357370
UpdateContractTemplate struct {
358371
ID string
359-
Script string
360372
Index int
373+
Title string
374+
Script string
361375
}
362376
}
363377

e2eTest/contract_templates_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/google/uuid"
2424
"github.com/stretchr/testify/assert"
2525
"github.com/stretchr/testify/require"
26+
"strconv"
2627
"testing"
2728
)
2829

@@ -208,6 +209,66 @@ func TestContractTemplates(t *testing.T) {
208209
assert.Equal(t, respB.UpdateContractTemplate.Script, respC.UpdateContractTemplate.Script)
209210
})
210211

212+
t.Run("Test same contract ordering after updating", func(t *testing.T) {
213+
c := newClient()
214+
215+
project := createProject(t, c)
216+
217+
var index4ID string
218+
219+
// Create 5 contracts
220+
for i := 1; i <= 5; i++ {
221+
var respA CreateContractTemplateResponse
222+
err := c.Post(
223+
MutationCreateContractTemplate,
224+
&respA,
225+
client.Var("projectId", project.ID),
226+
client.Var("title", "Contract "+strconv.Itoa(i)),
227+
client.Var("script", "apple"),
228+
client.AddCookie(c.SessionCookie()),
229+
)
230+
require.NoError(t, err)
231+
232+
if i == 4 {
233+
index4ID = respA.CreateContractTemplate.ID
234+
}
235+
}
236+
237+
var respC UpdateContractTemplateResponse
238+
err := c.Post(
239+
MutationUpdateContractTemplateTitle,
240+
&respC,
241+
client.Var("templateId", index4ID),
242+
client.Var("projectId", project.ID),
243+
client.Var("title", "Test Contract"),
244+
client.AddCookie(c.SessionCookie()),
245+
)
246+
require.NoError(t, err)
247+
248+
var respD UpdateContractTemplateResponse
249+
err = c.Post(
250+
MutationUpdateContractTemplateScript,
251+
&respD,
252+
client.Var("templateId", index4ID),
253+
client.Var("projectId", project.ID),
254+
client.Var("script", "Contract5"),
255+
client.AddCookie(c.SessionCookie()),
256+
)
257+
require.NoError(t, err)
258+
259+
var templatesResp GetProjectContractTemplatesResponse
260+
err = c.Post(
261+
QueryGetProjectContractTemplates,
262+
&templatesResp,
263+
client.Var("projectId", project.ID),
264+
client.AddCookie(c.SessionCookie()),
265+
)
266+
require.NoError(t, err)
267+
268+
// Assert the ordering is correct
269+
assert.Equal(t, "Test Contract", templatesResp.Project.ContractTemplates[3].Title)
270+
})
271+
211272
t.Run("Update non-existent contract template", func(t *testing.T) {
212273
c := newClient()
213274

storage/sql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ func (s *SQL) InsertTransactionExecution(exe *model.TransactionExecution) error
491491

492492
func (s *SQL) GetTransactionExecutionsForProject(projectID uuid.UUID, exes *[]*model.TransactionExecution) error {
493493
return s.db.Where(&model.TransactionExecution{File: model.File{ProjectID: projectID}}).
494-
Order("\"index\" asc").
495494
Find(exes).
495+
Order("\"index\" asc").
496496
Error
497497
}
498498

0 commit comments

Comments
 (0)