@things-factory/pdf 8.0.0-beta.9 → 8.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/bootstrap.js +6 -0
- package/client/pages/pdf-release/pdf-release-importer.ts +87 -0
- package/client/pages/pdf-release/pdf-release-list-page.ts +398 -0
- package/client/pages/pdf-template/pdf-template-importer.ts +87 -0
- package/client/pages/pdf-template/pdf-template-list-page.ts +491 -0
- package/client/route.ts +11 -0
- package/client/tsconfig.json +13 -0
- package/dist-client/tsconfig.tsbuildinfo +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/server/controller/pdf-service.ts +35 -0
- package/server/index.ts +3 -0
- package/server/routers/pdf-private-router.ts +85 -0
- package/server/routers/pdf-public-router.ts +80 -0
- package/server/routers/proxy-router.ts +9 -0
- package/server/routes.ts +19 -0
- package/server/service/index.ts +38 -0
- package/server/service/pdf-generate/pdf-generate-resolver.ts +81 -0
- package/server/service/pdf-release/index.ts +7 -0
- package/server/service/pdf-release/pdf-release-mutation.ts +138 -0
- package/server/service/pdf-release/pdf-release-query.ts +51 -0
- package/server/service/pdf-release/pdf-release-type.ts +55 -0
- package/server/service/pdf-release/pdf-release.ts +103 -0
- package/server/service/pdf-template/index.ts +7 -0
- package/server/service/pdf-template/pdf-template-mutation.ts +138 -0
- package/server/service/pdf-template/pdf-template-query.ts +51 -0
- package/server/service/pdf-template/pdf-template-type.ts +87 -0
- package/server/service/pdf-template/pdf-template.ts +108 -0
- package/server/tsconfig.json +10 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@things-factory/pdf",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.0.2",
|
|
4
4
|
"main": "dist-server/index.js",
|
|
5
5
|
"browser": "dist-client/index.js",
|
|
6
6
|
"things-factory": true,
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"clean": "npm run clean:server && npm run clean:client"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@things-factory/auth-base": "^8.0.
|
|
30
|
-
"@things-factory/utils": "^8.0.0
|
|
29
|
+
"@things-factory/auth-base": "^8.0.2",
|
|
30
|
+
"@things-factory/utils": "^8.0.0",
|
|
31
31
|
"await-spawn": "^4.0.1",
|
|
32
32
|
"ejs": "^3.1.10",
|
|
33
33
|
"handlebars": "^4.7.8",
|
|
34
34
|
"pdf-lib": "^1.17.1"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "39d60f56e142561233ddf6d47b539c637971357c"
|
|
37
37
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as ejs from 'ejs'
|
|
2
|
+
import { PDFDocument } from 'pdf-lib'
|
|
3
|
+
import { PDFTemplate } from '../service/pdf-template/pdf-template'
|
|
4
|
+
|
|
5
|
+
export class PdfService {
|
|
6
|
+
async generatePdf(template: PDFTemplate, data: any): Promise<Buffer> {
|
|
7
|
+
const pdfDoc = await PDFDocument.create()
|
|
8
|
+
|
|
9
|
+
// 템플릿 렌더링
|
|
10
|
+
const renderedContent = await ejs.render(template.content_template || '', data)
|
|
11
|
+
|
|
12
|
+
// PDF 생성
|
|
13
|
+
const page = pdfDoc.addPage()
|
|
14
|
+
const { width, height } = page.getSize()
|
|
15
|
+
page.drawText(renderedContent, {
|
|
16
|
+
x: 50,
|
|
17
|
+
y: height - 50,
|
|
18
|
+
size: 12
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
// 추가적으로 header, footer, cover 등의 렌더링 및 PDF 구성 가능
|
|
22
|
+
if (template.header_template) {
|
|
23
|
+
const renderedHeader = await ejs.render(template.header_template, data)
|
|
24
|
+
// Header 처리 로직
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (template.footer_template) {
|
|
28
|
+
const renderedFooter = await ejs.render(template.footer_template, data)
|
|
29
|
+
// Footer 처리 로직
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const pdfBytes = await pdfDoc.save()
|
|
33
|
+
return Buffer.from(pdfBytes)
|
|
34
|
+
}
|
|
35
|
+
}
|
package/server/index.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import Router from 'koa-router'
|
|
2
|
+
import * as fs from 'fs'
|
|
3
|
+
import * as path from 'path'
|
|
4
|
+
import send from 'koa-send'
|
|
5
|
+
|
|
6
|
+
import { PDFRelease } from '../service/pdf-release/pdf-release'
|
|
7
|
+
import { PDFTemplate } from '../service/pdf-template/pdf-template'
|
|
8
|
+
import { PdfService } from '../controller/pdf-service'
|
|
9
|
+
|
|
10
|
+
const PDF_STORAGE_PATH = '/path/to/pdf/storage' // 실제 파일 저장 경로
|
|
11
|
+
const PDF_BASE_URL = 'https://yourdomain.com/pdf/' // 파일 접근 URL의 기본 경로
|
|
12
|
+
|
|
13
|
+
const pdfService = new PdfService()
|
|
14
|
+
|
|
15
|
+
export const pdfPrivateRouter = new Router()
|
|
16
|
+
|
|
17
|
+
function parseQuery(query) {
|
|
18
|
+
for (const key in query) {
|
|
19
|
+
if (query.hasOwnProperty(key)) {
|
|
20
|
+
try {
|
|
21
|
+
query[key] = JSON.parse(query[key])
|
|
22
|
+
} catch (error) {
|
|
23
|
+
// do nothing
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return query
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// PDF 생성 및 저장 라우터
|
|
32
|
+
pdfPrivateRouter.post('/generate-pdf', async ctx => {
|
|
33
|
+
const { templateId, data } = ctx.request.body
|
|
34
|
+
const { domain, user, tx } = ctx.state
|
|
35
|
+
|
|
36
|
+
const templateRepository = tx.getRepository(PDFTemplate)
|
|
37
|
+
const releaseRepository = tx.getRepository(PDFRelease)
|
|
38
|
+
|
|
39
|
+
const template = await templateRepository.findOne(templateId)
|
|
40
|
+
if (!template) {
|
|
41
|
+
ctx.throw(404, 'PDF Template not found')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// EJS 템플릿을 사용하여 PDF 생성
|
|
45
|
+
const pdfBuffer = await pdfService.generatePdf(template, data)
|
|
46
|
+
|
|
47
|
+
// 파일 저장 경로 및 URL 생성
|
|
48
|
+
const fileName = `${template.name}-${Date.now()}.pdf`
|
|
49
|
+
const filePath = path.join(PDF_STORAGE_PATH, fileName)
|
|
50
|
+
const fileUrl = `${PDF_BASE_URL}${fileName}`
|
|
51
|
+
|
|
52
|
+
// 파일 저장
|
|
53
|
+
fs.writeFileSync(filePath, pdfBuffer)
|
|
54
|
+
|
|
55
|
+
// 발행 이력 기록
|
|
56
|
+
const release = releaseRepository.create({
|
|
57
|
+
template,
|
|
58
|
+
filePath,
|
|
59
|
+
fileUrl,
|
|
60
|
+
state: 'published',
|
|
61
|
+
releasedBy: user,
|
|
62
|
+
domain
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
await releaseRepository.save(release)
|
|
66
|
+
|
|
67
|
+
ctx.body = release
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
// PDF 다운로드 라우터
|
|
71
|
+
pdfPrivateRouter.get('/download-pdf/:id', async ctx => {
|
|
72
|
+
const { id } = ctx.params
|
|
73
|
+
const { tx } = ctx.state
|
|
74
|
+
|
|
75
|
+
const releaseRepository = tx.getRepository(PDFRelease)
|
|
76
|
+
const pdfRelease = await releaseRepository.findOne(id)
|
|
77
|
+
|
|
78
|
+
if (!pdfRelease || !pdfRelease.filePath || !fs.existsSync(pdfRelease.filePath)) {
|
|
79
|
+
ctx.throw(404, 'PDF file not found')
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// PDF 파일을 클라이언트에 전송
|
|
83
|
+
ctx.attachment(path.basename(pdfRelease.filePath))
|
|
84
|
+
await send(ctx, pdfRelease.filePath, { root: '/' })
|
|
85
|
+
})
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import await_spawn from 'await-spawn'
|
|
2
|
+
import { promises as fs } from 'fs'
|
|
3
|
+
import Router from 'koa-router'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
import { withTempDir } from '@things-factory/utils'
|
|
6
|
+
|
|
7
|
+
export const pdfPublicRouter = new Router()
|
|
8
|
+
|
|
9
|
+
/* to-png */
|
|
10
|
+
pdfPublicRouter.get('/to-png', async (context, next) => {
|
|
11
|
+
const { url } = context.query
|
|
12
|
+
|
|
13
|
+
const images = await withTempDir(async tmpDir => {
|
|
14
|
+
const curl = await_spawn('curl', ['-s', url])
|
|
15
|
+
|
|
16
|
+
await await_spawn('gs', ['-q', '-sDEVICE=png16m', '-o', `${tmpDir}/%d.png`, '-r300', '-'], {
|
|
17
|
+
stdio: [curl.child.stdout, 'pipe', 'pipe']
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const files = await fs.readdir(tmpDir)
|
|
21
|
+
return Promise.all(
|
|
22
|
+
files
|
|
23
|
+
.sort(new Intl.Collator(undefined, { numeric: true }).compare)
|
|
24
|
+
.map(filename => fs.readFile(path.join(tmpDir, filename)))
|
|
25
|
+
)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
/* TODO make it possible to return multiple images */
|
|
29
|
+
context.type = 'image/png'
|
|
30
|
+
context.body = images[0]
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
/* merge-pdf */
|
|
34
|
+
const mergePDFfiles = async urls => {
|
|
35
|
+
return await withTempDir(async tmpdir => {
|
|
36
|
+
const sources = typeof urls == 'string' ? [urls] : urls
|
|
37
|
+
const output = path.join(tmpdir, 'output.pdf')
|
|
38
|
+
const inputs = sources.map((source, idx) => path.join(tmpdir, `${idx}.pdf`))
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
await Promise.all(
|
|
42
|
+
sources.map((source, idx) => await_spawn('curl', ['-sSL', source, '-o', path.join(tmpdir, `${idx}.pdf`)]))
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
await await_spawn('gs', [
|
|
46
|
+
'-q',
|
|
47
|
+
'-sDEVICE=pdfwrite',
|
|
48
|
+
'-dAutoRotatePages=/None',
|
|
49
|
+
'-dNOPAUSE',
|
|
50
|
+
'-dBATCH',
|
|
51
|
+
'-dSAFER',
|
|
52
|
+
`-sOutputFile=${output}`,
|
|
53
|
+
...inputs
|
|
54
|
+
])
|
|
55
|
+
|
|
56
|
+
return await fs.readFile(output)
|
|
57
|
+
} catch (e) {
|
|
58
|
+
console.error('mergePDFfiles', 'error', e)
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
pdfPublicRouter.get('/merge', async (context, next) => {
|
|
64
|
+
const searchParams = new URLSearchParams(context.querystring)
|
|
65
|
+
const urls = searchParams.getAll('urls')
|
|
66
|
+
|
|
67
|
+
const pdf = await mergePDFfiles(urls)
|
|
68
|
+
|
|
69
|
+
context.type = 'application/pdf'
|
|
70
|
+
context.body = pdf
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
pdfPublicRouter.post('/merge', async (context, next) => {
|
|
74
|
+
const { urls } = context.request.body
|
|
75
|
+
|
|
76
|
+
const pdf = await mergePDFfiles(urls)
|
|
77
|
+
|
|
78
|
+
context.type = 'application/pdf'
|
|
79
|
+
context.body = pdf
|
|
80
|
+
})
|
package/server/routes.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { config } from '@things-factory/env'
|
|
2
|
+
import { pdfPublicRouter } from './routers/pdf-public-router'
|
|
3
|
+
import { pdfPrivateRouter } from './routers/pdf-private-router'
|
|
4
|
+
import { proxyRouter } from './routers/proxy-router'
|
|
5
|
+
|
|
6
|
+
const isPathBaseDomain = !config.get('subdomain') && !config.get('useVirtualHostBasedDomain')
|
|
7
|
+
|
|
8
|
+
process.on('bootstrap-module-global-public-route' as any, (app, globalPublicRouter) => {
|
|
9
|
+
globalPublicRouter.use('/pdf', pdfPublicRouter.routes(), pdfPublicRouter.allowedMethods())
|
|
10
|
+
globalPublicRouter.use('/proxy', proxyRouter.routes(), proxyRouter.allowedMethods())
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
process.on('bootstrap-module-domain-private-route' as any, (app, domainPrivateRouter) => {
|
|
14
|
+
if (isPathBaseDomain) {
|
|
15
|
+
domainPrivateRouter.use('/domain/:domain', pdfPrivateRouter.routes(), pdfPrivateRouter.allowedMethods())
|
|
16
|
+
} else {
|
|
17
|
+
domainPrivateRouter.use('', pdfPrivateRouter.routes(), pdfPrivateRouter.allowedMethods())
|
|
18
|
+
}
|
|
19
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/* EXPORT ENTITY TYPES */
|
|
2
|
+
export * from './pdf-release/pdf-release'
|
|
3
|
+
export * from './pdf-template/pdf-template'
|
|
4
|
+
|
|
5
|
+
/* IMPORT ENTITIES AND RESOLVERS */
|
|
6
|
+
import {
|
|
7
|
+
entities as PdfReleaseEntities,
|
|
8
|
+
resolvers as PdfReleaseResolvers,
|
|
9
|
+
subscribers as PdfReleaseSubscribers
|
|
10
|
+
} from './pdf-release'
|
|
11
|
+
import {
|
|
12
|
+
entities as PDFTemplateEntities,
|
|
13
|
+
resolvers as PDFTemplateResolvers,
|
|
14
|
+
subscribers as PDFTemplateSubscribers
|
|
15
|
+
} from './pdf-template'
|
|
16
|
+
|
|
17
|
+
export const entities = [
|
|
18
|
+
/* ENTITIES */
|
|
19
|
+
...PdfReleaseEntities,
|
|
20
|
+
...PdfReleaseEntities,
|
|
21
|
+
...PDFTemplateEntities
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
export const subscribers = [
|
|
25
|
+
/* SUBSCRIBERS */
|
|
26
|
+
...PdfReleaseSubscribers,
|
|
27
|
+
...PdfReleaseSubscribers,
|
|
28
|
+
...PDFTemplateSubscribers
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
export const schema = {
|
|
32
|
+
resolverClasses: [
|
|
33
|
+
/* RESOLVER CLASSES */
|
|
34
|
+
...PdfReleaseResolvers,
|
|
35
|
+
...PdfReleaseResolvers,
|
|
36
|
+
...PDFTemplateResolvers
|
|
37
|
+
]
|
|
38
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Resolver, Mutation, Arg, Ctx, Query } from 'type-graphql'
|
|
2
|
+
import * as fs from 'fs'
|
|
3
|
+
import * as path from 'path'
|
|
4
|
+
|
|
5
|
+
import { PDFRelease, PDFReleaseStatus } from '../pdf-release/pdf-release'
|
|
6
|
+
import { PDFTemplate } from '../pdf-template/pdf-template'
|
|
7
|
+
import { PdfService } from '../../controller/pdf-service'
|
|
8
|
+
import send from 'koa-send'
|
|
9
|
+
|
|
10
|
+
const PDF_STORAGE_PATH = '/path/to/pdf/storage' // 실제 파일 저장 경로
|
|
11
|
+
const PDF_BASE_URL = 'https://yourdomain.com/pdf/' // 파일 접근 URL의 기본 경로
|
|
12
|
+
|
|
13
|
+
@Resolver()
|
|
14
|
+
export class PDFGenerateResolver {
|
|
15
|
+
private pdfService = new PdfService()
|
|
16
|
+
|
|
17
|
+
@Mutation(() => PDFRelease)
|
|
18
|
+
async generatePDF(
|
|
19
|
+
@Arg('templateId') templateId: string,
|
|
20
|
+
@Arg('data') data: any,
|
|
21
|
+
@Ctx() ctx: ResolverContext
|
|
22
|
+
): Promise<PDFRelease> {
|
|
23
|
+
const { domain, user, tx } = ctx.state
|
|
24
|
+
const templateRepository = tx.getRepository(PDFTemplate)
|
|
25
|
+
const releaseRepository = tx.getRepository(PDFRelease)
|
|
26
|
+
|
|
27
|
+
const template = await templateRepository.findOne({ where: { id: templateId, domain: { id: domain.id } } })
|
|
28
|
+
if (!template) {
|
|
29
|
+
throw new Error('PDF Template not found')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
var state: PDFReleaseStatus = PDFReleaseStatus.published
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
const pdfBuffer = await this.pdfService.generatePdf(template, data)
|
|
36
|
+
|
|
37
|
+
const fileName = `${template.name}-${Date.now()}.pdf`
|
|
38
|
+
const filePath = path.join(PDF_STORAGE_PATH, fileName)
|
|
39
|
+
const fileUrl = `${PDF_BASE_URL}${fileName}`
|
|
40
|
+
|
|
41
|
+
fs.writeFileSync(filePath, pdfBuffer)
|
|
42
|
+
|
|
43
|
+
return releaseRepository.save({
|
|
44
|
+
template,
|
|
45
|
+
filePath,
|
|
46
|
+
fileUrl,
|
|
47
|
+
state,
|
|
48
|
+
creator: user,
|
|
49
|
+
updater: user,
|
|
50
|
+
domain
|
|
51
|
+
})
|
|
52
|
+
} catch (error) {
|
|
53
|
+
state = PDFReleaseStatus.failed
|
|
54
|
+
return releaseRepository.save({
|
|
55
|
+
template,
|
|
56
|
+
state,
|
|
57
|
+
creator: user,
|
|
58
|
+
updater: user,
|
|
59
|
+
domain
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@Query(() => String, { description: 'Download the generated PDF file' })
|
|
65
|
+
async downloadPDF(@Arg('id') id: string, @Ctx() ctx: ResolverContext): Promise<string> {
|
|
66
|
+
const { domain, user, tx } = ctx.state
|
|
67
|
+
|
|
68
|
+
const releaseRepository = tx.getRepository(PDFRelease)
|
|
69
|
+
const pdfRelease = await releaseRepository.findOne({ where: { id, domain: { id: domain.id } } })
|
|
70
|
+
|
|
71
|
+
if (!pdfRelease || !pdfRelease.filePath || !fs.existsSync(pdfRelease.filePath)) {
|
|
72
|
+
throw new Error('PDF file not found')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// PDF 파일을 클라이언트에 전송
|
|
76
|
+
ctx.attachment(path.basename(pdfRelease.filePath))
|
|
77
|
+
await send(ctx, pdfRelease.filePath, { root: '/' })
|
|
78
|
+
|
|
79
|
+
return 'Download started'
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PDFRelease } from './pdf-release'
|
|
2
|
+
import { PDFReleaseQuery } from './pdf-release-query'
|
|
3
|
+
import { PDFReleaseMutation } from './pdf-release-mutation'
|
|
4
|
+
|
|
5
|
+
export const entities = [PDFRelease]
|
|
6
|
+
export const resolvers = [PDFReleaseQuery, PDFReleaseMutation]
|
|
7
|
+
export const subscribers = []
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Resolver, Mutation, Arg, Ctx, Directive } from 'type-graphql'
|
|
2
|
+
import { In } from 'typeorm'
|
|
3
|
+
|
|
4
|
+
import { PDFRelease } from './pdf-release'
|
|
5
|
+
import { NewPDFRelease, PDFReleasePatch } from './pdf-release-type'
|
|
6
|
+
|
|
7
|
+
@Resolver(PDFRelease)
|
|
8
|
+
export class PDFReleaseMutation {
|
|
9
|
+
@Directive('@transaction')
|
|
10
|
+
@Mutation(returns => PDFRelease, { description: 'To create new PDFRelease' })
|
|
11
|
+
async createPDFRelease(
|
|
12
|
+
@Arg('pdfRelease') pdfRelease: NewPDFRelease,
|
|
13
|
+
@Ctx() context: ResolverContext
|
|
14
|
+
): Promise<PDFRelease> {
|
|
15
|
+
const { domain, user, tx } = context.state
|
|
16
|
+
|
|
17
|
+
const result = await tx.getRepository(PDFRelease).save({
|
|
18
|
+
...pdfRelease,
|
|
19
|
+
domain,
|
|
20
|
+
creator: user,
|
|
21
|
+
updater: user
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
return result
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@Directive('@transaction')
|
|
28
|
+
@Mutation(returns => PDFRelease, { description: 'To modify PDFRelease information' })
|
|
29
|
+
async updatePDFRelease(
|
|
30
|
+
@Arg('id') id: string,
|
|
31
|
+
@Arg('patch') patch: PDFReleasePatch,
|
|
32
|
+
@Ctx() context: ResolverContext
|
|
33
|
+
): Promise<PDFRelease> {
|
|
34
|
+
const { domain, user, tx } = context.state
|
|
35
|
+
|
|
36
|
+
const repository = tx.getRepository(PDFRelease)
|
|
37
|
+
const pdfRelease = await repository.findOne({
|
|
38
|
+
where: { domain: { id: domain.id }, id }
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const result = await repository.save({
|
|
42
|
+
...pdfRelease,
|
|
43
|
+
...patch,
|
|
44
|
+
updater: user
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return result
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@Directive('@transaction')
|
|
51
|
+
@Mutation(returns => [PDFRelease], { description: "To modify multiple PDFReleases' information" })
|
|
52
|
+
async updateMultiplePDFRelease(
|
|
53
|
+
@Arg('patches', type => [PDFReleasePatch]) patches: PDFReleasePatch[],
|
|
54
|
+
@Ctx() context: ResolverContext
|
|
55
|
+
): Promise<PDFRelease[]> {
|
|
56
|
+
const { domain, user, tx } = context.state
|
|
57
|
+
|
|
58
|
+
let results = []
|
|
59
|
+
const _createRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === '+')
|
|
60
|
+
const _updateRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === 'M')
|
|
61
|
+
const pdfReleaseRepo = tx.getRepository(PDFRelease)
|
|
62
|
+
|
|
63
|
+
if (_createRecords.length > 0) {
|
|
64
|
+
for (let i = 0; i < _createRecords.length; i++) {
|
|
65
|
+
const newRecord = _createRecords[i]
|
|
66
|
+
|
|
67
|
+
const result = await pdfReleaseRepo.save({
|
|
68
|
+
...newRecord,
|
|
69
|
+
domain,
|
|
70
|
+
creator: user,
|
|
71
|
+
updater: user
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
results.push({ ...result, cuFlag: '+' })
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (_updateRecords.length > 0) {
|
|
79
|
+
for (let i = 0; i < _updateRecords.length; i++) {
|
|
80
|
+
const updateRecord = _updateRecords[i]
|
|
81
|
+
const pdfRelease = await pdfReleaseRepo.findOneBy({ id: updateRecord.id })
|
|
82
|
+
|
|
83
|
+
const result = await pdfReleaseRepo.save({
|
|
84
|
+
...pdfRelease,
|
|
85
|
+
...updateRecord,
|
|
86
|
+
updater: user
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
results.push({ ...result, cuFlag: 'M' })
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return results
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Directive('@transaction')
|
|
97
|
+
@Mutation(returns => Boolean, { description: 'To delete PDFRelease' })
|
|
98
|
+
async deletePDFRelease(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<boolean> {
|
|
99
|
+
const { domain, tx } = context.state
|
|
100
|
+
|
|
101
|
+
await tx.getRepository(PDFRelease).delete({ domain: { id: domain.id }, id })
|
|
102
|
+
|
|
103
|
+
return true
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
@Directive('@transaction')
|
|
107
|
+
@Mutation(returns => Boolean, { description: 'To delete multiple PDFReleases' })
|
|
108
|
+
async deletePDFReleases(
|
|
109
|
+
@Arg('ids', type => [String]) ids: string[],
|
|
110
|
+
@Ctx() context: ResolverContext
|
|
111
|
+
): Promise<boolean> {
|
|
112
|
+
const { domain, tx } = context.state
|
|
113
|
+
|
|
114
|
+
await tx.getRepository(PDFRelease).delete({
|
|
115
|
+
domain: { id: domain.id },
|
|
116
|
+
id: In(ids)
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
return true
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
@Directive('@transaction')
|
|
123
|
+
@Mutation(returns => Boolean, { description: 'To import multiple PDFReleases' })
|
|
124
|
+
async importPDFReleases(
|
|
125
|
+
@Arg('pdfReleases', type => [PDFReleasePatch]) pdfReleases: PDFReleasePatch[],
|
|
126
|
+
@Ctx() context: ResolverContext
|
|
127
|
+
): Promise<boolean> {
|
|
128
|
+
const { domain, tx } = context.state
|
|
129
|
+
|
|
130
|
+
await Promise.all(
|
|
131
|
+
pdfReleases.map(async (pdfRelease: PDFReleasePatch) => {
|
|
132
|
+
const createdPDFRelease: PDFRelease = await tx.getRepository(PDFRelease).save({ domain, ...pdfRelease })
|
|
133
|
+
})
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
return true
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Resolver, Query, FieldResolver, Root, Args, Arg, Ctx, Directive } from 'type-graphql'
|
|
2
|
+
import { Domain, getQueryBuilderFromListParams, getRepository, ListParam } from '@things-factory/shell'
|
|
3
|
+
import { User } from '@things-factory/auth-base'
|
|
4
|
+
import { PDFRelease } from './pdf-release'
|
|
5
|
+
import { PDFReleaseList } from './pdf-release-type'
|
|
6
|
+
|
|
7
|
+
@Resolver(PDFRelease)
|
|
8
|
+
export class PDFReleaseQuery {
|
|
9
|
+
@Query(returns => PDFRelease!, { nullable: true, description: 'To fetch a PDFRelease' })
|
|
10
|
+
async pdfRelease(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<PDFRelease> {
|
|
11
|
+
const { domain } = context.state
|
|
12
|
+
|
|
13
|
+
return await getRepository(PDFRelease).findOne({
|
|
14
|
+
where: { domain: { id: domain.id }, id }
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@Query(returns => PDFReleaseList, { description: 'To fetch multiple PDFReleases' })
|
|
19
|
+
async pdfReleases(
|
|
20
|
+
@Args(type => ListParam) params: ListParam,
|
|
21
|
+
@Ctx() context: ResolverContext
|
|
22
|
+
): Promise<PDFReleaseList> {
|
|
23
|
+
const { domain } = context.state
|
|
24
|
+
|
|
25
|
+
const queryBuilder = getQueryBuilderFromListParams({
|
|
26
|
+
domain,
|
|
27
|
+
params,
|
|
28
|
+
repository: await getRepository(PDFRelease),
|
|
29
|
+
searchables: ['name', 'description']
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const [items, total] = await queryBuilder.getManyAndCount()
|
|
33
|
+
|
|
34
|
+
return { items, total }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@FieldResolver(type => Domain)
|
|
38
|
+
async domain(@Root() pdfRelease: PDFRelease): Promise<Domain> {
|
|
39
|
+
return pdfRelease.domainId && (await getRepository(Domain).findOneBy({ id: pdfRelease.domainId }))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@FieldResolver(type => User)
|
|
43
|
+
async updater(@Root() pdfRelease: PDFRelease): Promise<User> {
|
|
44
|
+
return pdfRelease.updaterId && (await getRepository(User).findOneBy({ id: pdfRelease.updaterId }))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@FieldResolver(type => User)
|
|
48
|
+
async creator(@Root() pdfRelease: PDFRelease): Promise<User> {
|
|
49
|
+
return pdfRelease.creatorId && (await getRepository(User).findOneBy({ id: pdfRelease.creatorId }))
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { FileUpload } from 'graphql-upload/GraphQLUpload.js'
|
|
2
|
+
import GraphQLUpload from 'graphql-upload/GraphQLUpload.js'
|
|
3
|
+
import { ObjectType, Field, InputType, Int, ID, registerEnumType } from 'type-graphql'
|
|
4
|
+
|
|
5
|
+
import { ObjectRef, ScalarObject } from '@things-factory/shell'
|
|
6
|
+
|
|
7
|
+
import { PDFRelease, PDFReleaseStatus } from './pdf-release'
|
|
8
|
+
|
|
9
|
+
@InputType()
|
|
10
|
+
export class NewPDFRelease {
|
|
11
|
+
@Field()
|
|
12
|
+
name: string
|
|
13
|
+
|
|
14
|
+
@Field({ nullable: true })
|
|
15
|
+
description?: string
|
|
16
|
+
|
|
17
|
+
@Field(type => PDFReleaseStatus, { nullable: true })
|
|
18
|
+
state?: PDFReleaseStatus
|
|
19
|
+
|
|
20
|
+
@Field({ nullable: true })
|
|
21
|
+
active?: boolean
|
|
22
|
+
|
|
23
|
+
@Field({ nullable: true })
|
|
24
|
+
params?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@InputType()
|
|
28
|
+
export class PDFReleasePatch {
|
|
29
|
+
@Field(type => ID, { nullable: true })
|
|
30
|
+
id?: string
|
|
31
|
+
|
|
32
|
+
@Field({ nullable: true })
|
|
33
|
+
name?: string
|
|
34
|
+
|
|
35
|
+
@Field({ nullable: true })
|
|
36
|
+
description?: string
|
|
37
|
+
|
|
38
|
+
@Field(type => PDFReleaseStatus, { nullable: true })
|
|
39
|
+
state?: PDFReleaseStatus
|
|
40
|
+
|
|
41
|
+
@Field({ nullable: true })
|
|
42
|
+
active?: boolean
|
|
43
|
+
|
|
44
|
+
@Field({ nullable: true })
|
|
45
|
+
cuFlag?: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@ObjectType()
|
|
49
|
+
export class PDFReleaseList {
|
|
50
|
+
@Field(type => [PDFRelease])
|
|
51
|
+
items: PDFRelease[]
|
|
52
|
+
|
|
53
|
+
@Field(type => Int)
|
|
54
|
+
total: number
|
|
55
|
+
}
|