11import type { PresignedPost } from "@aws-sdk/s3-presigned-post"
2- import { ArticleFilterQuerySchema , ArticleSchema , ArticleTagSchema , ArticleWriteSchema } from "@dotkomonline/ types"
2+ import { Article , ArticleTag , ArticleWrite } from "./article- types"
33import type { inferProcedureInput , inferProcedureOutput } from "@trpc/server"
44import { z } from "zod"
55import { isEditor } from "../../authorization"
66import { withAuditLogEntry , withAuthentication , withAuthorization , withDatabaseTransaction } from "../../middlewares"
77import { BasePaginateInputSchema , PaginateInputSchema } from "../../query"
88import { procedure , t } from "../../trpc"
9+ import { ArticleFilterQuery } from "./article-service"
10+ import { parseOutputType } from "src/invariant"
911
10- export type CreateArticleInput = inferProcedureInput < typeof createArticleProcedure >
11- export type CreateArticleOutput = inferProcedureOutput < typeof createArticleProcedure >
12- const createArticleProcedure = procedure
13- . input (
14- z . object ( {
15- article : ArticleWriteSchema ,
16- tags : z . array ( ArticleTagSchema . shape . name ) ,
17- } )
18- )
19- . use ( withAuthentication ( ) )
20- . use ( withAuthorization ( isEditor ( ) ) )
21- . use ( withDatabaseTransaction ( ) )
22- . use ( withAuditLogEntry ( ) )
23- . mutation ( async ( { input , ctx } ) => {
24- const article = await ctx . articleService . create ( ctx . handle , input . article )
25- const tags = await ctx . articleService . setTags ( ctx . handle , article . id , input . tags )
26- return {
27- ... article ,
28- tags ,
29- }
12+ export const ArticleMessage = Article . pick ( {
13+ id : true ,
14+ slug : true ,
15+ title : true ,
16+ author : true ,
17+ photographer : true ,
18+ imageUrl : true ,
19+ excerpt : true ,
20+ content : true ,
21+ isFeatured : true ,
22+ vimeoId : true ,
23+ createdAt : true ,
24+ updatedAt : true ,
25+ tags : true ,
26+ } )
27+
28+ function buildCreateArticleProcedure ( ) {
29+ const Input = z . object ( {
30+ article : ArticleWrite ,
31+ tags : z . array ( ArticleTag . shape . name ) ,
3032 } )
33+ const Output = ArticleMessage
3134
32- export type EditArticleInput = inferProcedureInput < typeof editArticleProcedure >
33- export type EditArticleOutput = inferProcedureOutput < typeof editArticleProcedure >
34- const editArticleProcedure = procedure
35- . input (
36- z . object ( {
37- id : ArticleSchema . shape . id ,
38- input : ArticleWriteSchema . partial ( ) ,
39- tags : z . array ( ArticleTagSchema . shape . name ) ,
35+ return procedure
36+ . input ( Input )
37+ . output ( Output )
38+ . use ( withAuthentication ( ) )
39+ . use ( withAuthorization ( isEditor ( ) ) )
40+ . use ( withDatabaseTransaction ( ) )
41+ . use ( withAuditLogEntry ( ) )
42+ . mutation ( async ( { input, ctx } ) => {
43+ const { id } = await ctx . articleService . create ( ctx . handle , input . article )
44+ await ctx . articleService . setTags ( ctx . handle , id , input . tags )
45+ const article = await ctx . articleService . getById ( ctx . handle , id )
46+ return parseOutputType ( Output , article )
4047 } )
41- )
42- . use ( withAuthentication ( ) )
43- . use ( withAuthorization ( isEditor ( ) ) )
44- . use ( withDatabaseTransaction ( ) )
45- . use ( withAuditLogEntry ( ) )
46- . mutation ( async ( { input, ctx } ) => {
47- const article = await ctx . articleService . update ( ctx . handle , input . id , input . input )
48- const tags = await ctx . articleService . setTags ( ctx . handle , input . id , input . tags )
49- return { ...article , tags }
48+ }
49+
50+ function buildEditArticleProcedure ( ) {
51+ const Input = z . object ( {
52+ id : Article . shape . id ,
53+ input : ArticleWrite . partial ( ) ,
54+ tags : z . array ( ArticleTag . shape . name ) ,
5055 } )
56+ const Output = ArticleMessage
57+
58+ return procedure
59+ . input ( Input )
60+ . output ( Output )
61+ . use ( withAuthentication ( ) )
62+ . use ( withAuthorization ( isEditor ( ) ) )
63+ . use ( withDatabaseTransaction ( ) )
64+ . use ( withAuditLogEntry ( ) )
65+ . mutation ( async ( { input, ctx } ) => {
66+ const { id } = await ctx . articleService . update ( ctx . handle , input . id , input . input )
67+ await ctx . articleService . setTags ( ctx . handle , input . id , input . tags )
68+ const article = await ctx . articleService . getById ( ctx . handle , id )
69+ return parseOutputType ( Output , article )
70+ } )
71+ }
5172
5273export type AllArticlesInput = inferProcedureInput < typeof allArticlesProcedure >
5374export type AllArticlesOutput = inferProcedureOutput < typeof allArticlesProcedure >
@@ -59,7 +80,7 @@ const allArticlesProcedure = procedure
5980export type FindArticlesInput = inferProcedureInput < typeof findArticlesProcedure >
6081export type FindArticlesOutput = inferProcedureOutput < typeof findArticlesProcedure >
6182const findArticlesProcedure = procedure
62- . input ( BasePaginateInputSchema . extend ( { filters : ArticleFilterQuerySchema } ) )
83+ . input ( BasePaginateInputSchema . extend ( { filters : ArticleFilterQuery } ) )
6384 . use ( withDatabaseTransaction ( ) )
6485 . query ( async ( { input, ctx } ) => {
6586 const items = await ctx . articleService . findMany ( ctx . handle , input . filters , input )
@@ -73,21 +94,22 @@ const findArticlesProcedure = procedure
7394export type FindArticleInput = inferProcedureInput < typeof findArticleProcedure >
7495export type FindArticleOutput = inferProcedureOutput < typeof findArticleProcedure >
7596const findArticleProcedure = procedure
76- . input ( ArticleSchema . shape . id )
97+ . input ( Article . shape . id )
7798 . use ( withDatabaseTransaction ( ) )
7899 . query ( async ( { input, ctx } ) => ctx . articleService . findById ( ctx . handle , input ) )
79100
80101export type GetArticleInput = inferProcedureInput < typeof getArticleProcedure >
81102export type GetArticleOutput = inferProcedureOutput < typeof getArticleProcedure >
82103const getArticleProcedure = procedure
83- . input ( ArticleSchema . shape . id )
104+ . input ( Article . shape . id )
84105 . use ( withDatabaseTransaction ( ) )
85106 . query ( async ( { input, ctx } ) => ctx . articleService . getById ( ctx . handle , input ) )
86107
87108export type FindRelatedArticlesInput = inferProcedureInput < typeof findRelatedArticlesProcedure >
88109export type FindRelatedArticlesOutput = inferProcedureOutput < typeof findRelatedArticlesProcedure >
89110const findRelatedArticlesProcedure = procedure
90- . input ( ArticleSchema )
111+ // TODO: Accept article id here instead
112+ . input ( Article )
91113 . use ( withDatabaseTransaction ( ) )
92114 . query ( async ( { input, ctx } ) => ctx . articleService . findRelated ( ctx . handle , input ) )
93115
@@ -118,8 +140,8 @@ export type AddArticleTagOutput = inferProcedureOutput<typeof addArticleTagProce
118140const addArticleTagProcedure = procedure
119141 . input (
120142 z . object ( {
121- id : ArticleSchema . shape . id ,
122- tag : ArticleTagSchema . shape . name ,
143+ id : Article . shape . id ,
144+ tag : ArticleTag . shape . name ,
123145 } )
124146 )
125147 . use ( withAuthentication ( ) )
@@ -135,8 +157,8 @@ export type RemoveArticleTagOutput = inferProcedureOutput<typeof removeArticleTa
135157const removeArticleTagProcedure = procedure
136158 . input (
137159 z . object ( {
138- id : ArticleSchema . shape . id ,
139- tag : ArticleTagSchema . shape . name ,
160+ id : Article . shape . id ,
161+ tag : ArticleTag . shape . name ,
140162 } )
141163 )
142164 . use ( withAuthentication ( ) )
@@ -164,8 +186,8 @@ const createArticleFileUploadProcedure = procedure
164186 } )
165187
166188export const articleRouter = t . router ( {
167- create : createArticleProcedure ,
168- edit : editArticleProcedure ,
189+ create : buildCreateArticleProcedure ( ) ,
190+ edit : buildEditArticleProcedure ( ) ,
169191 all : allArticlesProcedure ,
170192 findArticles : findArticlesProcedure ,
171193 find : findArticleProcedure ,
0 commit comments