5
5
"errors"
6
6
"fmt"
7
7
8
+ "github.com/glasskube/distr/internal/util"
9
+
8
10
"github.com/glasskube/distr/internal/apierrors"
9
11
internalctx "github.com/glasskube/distr/internal/context"
10
12
"github.com/glasskube/distr/internal/types"
@@ -20,9 +22,20 @@ const (
20
22
coalesce((
21
23
SELECT array_agg(row(av.id, av.created_at, av.archived_at, av.name, av.application_id,
22
24
av.chart_type, av.chart_name, av.chart_url, av.chart_version) ORDER BY av.created_at ASC)
23
- FROM applicationversion av
25
+ FROM ApplicationVersion av
24
26
WHERE av.application_id = a.id
25
- ), array[]::record[]) as versions `
27
+ ), array[]::record[]) AS versions `
28
+
29
+ applicationWithLicensedVersionsOutputExpr = applicationOutputExpr + `,
30
+ coalesce((
31
+ SELECT array_agg(row(av.id, av.created_at, av.archived_at, av.name, av.application_id,
32
+ av.chart_type, av.chart_name, av.chart_url, av.chart_version) ORDER BY av.created_at ASC)
33
+ FROM ApplicationVersion av
34
+ WHERE av.application_id = a.id and
35
+ ((av.id IN
36
+ (SELECT application_version_id FROM ApplicationLicense_ApplicationVersion WHERE application_license_id = al.id)
37
+ OR (SELECT NOT EXISTS (SELECT FROM ApplicationLicense_ApplicationVersion WHERE application_license_id = al.id)))
38
+ )), array[]::record[]) AS versions `
26
39
)
27
40
28
41
func CreateApplication (ctx context.Context , application * types.Application , orgID uuid.UUID ) error {
@@ -88,22 +101,51 @@ func GetApplicationsByOrgID(ctx context.Context, orgID uuid.UUID) ([]types.Appli
88
101
}
89
102
}
90
103
104
+ func appendMissingVersions (application types.Application ,
105
+ versions []types.ApplicationVersion ) types.Application {
106
+
107
+ for _ , version := range versions {
108
+ found := false
109
+ for _ , existingVersion := range application .Versions {
110
+ if version .ID == existingVersion .ID {
111
+ found = true
112
+ break
113
+ }
114
+ }
115
+ if ! found {
116
+ application .Versions = append (application .Versions , version )
117
+ }
118
+ }
119
+ return application
120
+ }
121
+
122
+ func mergeApplications (applications []types.Application ) []types.Application {
123
+ applicationMap := make (map [uuid.UUID ]types.Application )
124
+ for _ , application := range applications {
125
+ if existingApplication , ok := applicationMap [application .ID ]; ok {
126
+ applicationMap [application .ID ] = appendMissingVersions (existingApplication , application .Versions )
127
+ } else {
128
+ applicationMap [application .ID ] = application
129
+ }
130
+ }
131
+ return util .GetValues (applicationMap )
132
+ }
133
+
91
134
func GetApplicationsWithLicenseOwnerID (ctx context.Context , id uuid.UUID ) ([]types.Application , error ) {
92
135
db := internalctx .GetDb (ctx )
93
- // TODO: Only include versions from at least one license
94
136
if rows , err := db .Query (ctx , `
95
- SELECT DISTINCT ` + applicationWithVersionsOutputExpr + `
137
+ SELECT DISTINCT ` + applicationWithLicensedVersionsOutputExpr + `
96
138
FROM ApplicationLicense al
97
139
LEFT JOIN Application a ON al.application_id = a.id
98
- WHERE al.owner_useraccount_id = @id
140
+ WHERE al.owner_useraccount_id = @id AND (al.expires_at IS NULL OR al.expires_at > now())
99
141
ORDER BY a.name
100
142
` , pgx.NamedArgs {"id" : id }); err != nil {
101
143
return nil , fmt .Errorf ("failed to query applications: %w" , err )
102
144
} else if applications , err :=
103
145
pgx .CollectRows (rows , pgx .RowToStructByName [types .Application ]); err != nil {
104
146
return nil , fmt .Errorf ("failed to get applications: %w" , err )
105
147
} else {
106
- return applications , nil
148
+ return mergeApplications ( applications ) , nil
107
149
}
108
150
}
109
151
@@ -126,6 +168,27 @@ func GetApplication(ctx context.Context, id, orgID uuid.UUID) (*types.Applicatio
126
168
}
127
169
}
128
170
171
+ func GetApplicationWithLicenseOwnerID (ctx context.Context , oID uuid.UUID , id uuid.UUID ) (* types.Application , error ) {
172
+ db := internalctx .GetDb (ctx )
173
+ if rows , err := db .Query (ctx , `
174
+ SELECT DISTINCT ` + applicationWithLicensedVersionsOutputExpr + `
175
+ FROM ApplicationLicense al
176
+ LEFT JOIN Application a ON al.application_id = a.id
177
+ WHERE al.owner_useraccount_id = @ownerID AND a.id = @id AND (al.expires_at IS NULL OR al.expires_at > now())
178
+ ORDER BY a.name
179
+ ` , pgx.NamedArgs {"ownerID" : oID , "id" : id }); err != nil {
180
+ return nil , fmt .Errorf ("failed to query applications: %w" , err )
181
+ } else if applications , err :=
182
+ pgx .CollectRows (rows , pgx .RowToStructByName [types .Application ]); err != nil {
183
+ if errors .Is (err , pgx .ErrNoRows ) {
184
+ return nil , apierrors .ErrNotFound
185
+ }
186
+ return nil , fmt .Errorf ("failed to get application: %w" , err )
187
+ } else {
188
+ return & mergeApplications (applications )[0 ], nil
189
+ }
190
+ }
191
+
129
192
func GetApplicationForApplicationVersionID (ctx context.Context , id , orgID uuid.UUID ) (* types.Application , error ) {
130
193
db := internalctx .GetDb (ctx )
131
194
if rows , err := db .Query (ctx , `
0 commit comments