@things-factory/id-rule-base 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.
@@ -0,0 +1,34 @@
1
+ import { Arg, Ctx, Mutation, Resolver } from 'type-graphql'
2
+
3
+ import { getRepository } from '@things-factory/shell'
4
+
5
+ import { IdRule } from './id-rule'
6
+ import { IdRulePatch, NewIdRule } from './id-rule-types'
7
+
8
+ @Resolver(IdRule)
9
+ export class IdRuleMutation {
10
+ @Mutation(returns => IdRule)
11
+ async createIdRule(@Arg('idRule') idRule: NewIdRule, @Ctx() context: any) {
12
+ return await getRepository(IdRule).save({
13
+ ...idRule,
14
+ domain: context.state.domain
15
+ })
16
+ }
17
+
18
+ @Mutation(returns => IdRule)
19
+ async updateIdRule(
20
+ @Arg('type') type: string,
21
+ @Arg('patch') patch: IdRulePatch,
22
+ @Ctx() context: any
23
+ ): Promise<IdRule> {
24
+ const repository = getRepository(IdRule)
25
+ const idRule = await repository.findOne({
26
+ where: { type: type as any, domain: { id: context.state.domain.id } }
27
+ })
28
+
29
+ return await repository.save({
30
+ ...idRule,
31
+ ...patch
32
+ })
33
+ }
34
+ }
@@ -0,0 +1,25 @@
1
+ import { Arg, Ctx, FieldResolver, Query, Resolver, Root } from 'type-graphql'
2
+
3
+ import { Domain, getRepository } from '@things-factory/shell'
4
+
5
+ import { IdRule } from './id-rule'
6
+
7
+ @Resolver(IdRule)
8
+ export class IdRuleQuery {
9
+ @Query(returns => IdRule, { nullable: true })
10
+ async idRule(@Arg('type') type: string, @Ctx() context: any): Promise<IdRule> {
11
+ const rule = await getRepository(IdRule).findOne({
12
+ where: {
13
+ domain: { id: context.state.domain.id },
14
+ type: type as any
15
+ }
16
+ })
17
+
18
+ return rule
19
+ }
20
+
21
+ @FieldResolver(type => Domain)
22
+ async domain(@Root() idRule: IdRule) {
23
+ return await getRepository(Domain).findOneBy({ id: idRule.domainId })
24
+ }
25
+ }
@@ -0,0 +1,20 @@
1
+ import { Field, InputType } from 'type-graphql'
2
+
3
+
4
+ @InputType()
5
+ export class IdRulePatch {
6
+ @Field({ nullable: true })
7
+ type?: string
8
+
9
+ @Field({ nullable: true })
10
+ rule?: string
11
+ }
12
+
13
+ @InputType()
14
+ export class NewIdRule {
15
+ @Field()
16
+ type: string
17
+
18
+ @Field()
19
+ rule: string
20
+ }
@@ -0,0 +1,49 @@
1
+ import {
2
+ Field,
3
+ ID,
4
+ ObjectType
5
+ } from 'type-graphql'
6
+ import {
7
+ Column,
8
+ CreateDateColumn,
9
+ Entity,
10
+ Index,
11
+ ManyToOne,
12
+ PrimaryGeneratedColumn,
13
+ RelationId,
14
+ UpdateDateColumn
15
+ } from 'typeorm'
16
+
17
+ import { Domain } from '@things-factory/shell'
18
+
19
+ @Entity()
20
+ @Index('ix_id_rule_1', (idRule: IdRule) => [idRule.domain])
21
+ @ObjectType()
22
+ export class IdRule {
23
+ @PrimaryGeneratedColumn('uuid')
24
+ @Field(type => ID)
25
+ readonly id: string
26
+
27
+ @ManyToOne(type => Domain)
28
+ @Field(type => Domain)
29
+ domain: Domain
30
+
31
+ @RelationId((idRule: IdRule) => idRule.domain)
32
+ domainId: string
33
+
34
+ @Column()
35
+ @Field({ nullable: true })
36
+ type: string
37
+
38
+ @Column()
39
+ @Field()
40
+ rule: string
41
+
42
+ @CreateDateColumn()
43
+ @Field({ nullable: true })
44
+ createdAt: Date
45
+
46
+ @UpdateDateColumn()
47
+ @Field({ nullable: true })
48
+ updatedAt: Date
49
+ }
@@ -0,0 +1,6 @@
1
+ import { IdRule } from './id-rule'
2
+ import { IdRuleMutation } from './id-rule-mutation'
3
+ import { IdRuleQuery } from './id-rule-query'
4
+
5
+ export const entities = [IdRule]
6
+ export const resolvers = [IdRuleQuery, IdRuleMutation]
@@ -0,0 +1,16 @@
1
+ import { entities as IdRuleEntities, resolvers as IdRuleResolvers } from './id-rule'
2
+ import { entities as SequenceEntities, resolvers as SequenceResolvers } from './sequence'
3
+ import { entities as DocNumberEntities, resolvers as DocNumberResolvers } from './doc-number'
4
+
5
+ /* EXPORT ENTITY TYPES */
6
+ export * from './id-rule/id-rule'
7
+ /* EXPORT TYPES */
8
+ export * from './id-rule/id-rule-types'
9
+ export * from './sequence/sequence'
10
+ export * from './doc-number/doc-number'
11
+
12
+ export const entities = [...IdRuleEntities, ...SequenceEntities, ...DocNumberEntities]
13
+
14
+ export const schema = {
15
+ resolverClasses: [...IdRuleResolvers, ...SequenceResolvers, ...DocNumberResolvers]
16
+ }
@@ -0,0 +1,5 @@
1
+ import { Sequence } from './sequence'
2
+ import { SequenceMutation } from './sequence-mutation'
3
+
4
+ export const entities = [Sequence]
5
+ export const resolvers = [SequenceMutation]
@@ -0,0 +1,56 @@
1
+ import { Arg, Ctx, Mutation, Resolver } from 'type-graphql'
2
+
3
+ import { getRepository } from '@things-factory/shell'
4
+
5
+ import { SEQUENCE_TYPE } from '../../constants'
6
+ import { Sequence } from './sequence'
7
+
8
+ @Resolver(Sequence)
9
+ export class SequenceMutation {
10
+ @Mutation(returns => Sequence)
11
+ async upsertSequence(
12
+ @Arg('type') type: string,
13
+ @Arg('counter') counter: number,
14
+ @Arg('bizplaceId') bizplaceId: string,
15
+ @Ctx() context: any
16
+ ) {
17
+ const { domain } = context.state
18
+ const seqRepo = getRepository(Sequence)
19
+ const now = new Date()
20
+ const expiresAt = new Date(now.setMonth(now.getMonth() + 12))
21
+
22
+ const patternObj: any = {
23
+ type,
24
+ date: new Date().toISOString().split('T')[0],
25
+ bizplaceId
26
+ }
27
+
28
+ const pattern: string = JSON.stringify(patternObj)
29
+
30
+ const foundSeq = await seqRepo.findOne({
31
+ where: { pattern, type: SEQUENCE_TYPE.PRODUCT_LABEL_COUNTER, domain: { id: domain.id } }
32
+ })
33
+
34
+ if (foundSeq) {
35
+ await seqRepo.update(
36
+ { id: foundSeq.id },
37
+ {
38
+ pattern,
39
+ seq: counter ? foundSeq.seq + counter : foundSeq.seq + 1,
40
+ expiresAt,
41
+ updatedAt: now
42
+ }
43
+ )
44
+
45
+ return await seqRepo.findOneBy({ id: foundSeq.id })
46
+ } else {
47
+ return await seqRepo.save({
48
+ domain,
49
+ pattern,
50
+ seq: counter || 1,
51
+ expiresAt,
52
+ type: SEQUENCE_TYPE.PRODUCT_LABEL_COUNTER
53
+ })
54
+ }
55
+ }
56
+ }
@@ -0,0 +1,49 @@
1
+ import { Field, ID, ObjectType } from 'type-graphql'
2
+ import { Column, CreateDateColumn, Entity, Index, ManyToOne, PrimaryGeneratedColumn, RelationId, UpdateDateColumn } from 'typeorm'
3
+
4
+ import { Domain } from '@things-factory/shell'
5
+
6
+ @Entity()
7
+ @Index('ix_sequence_0', (seq: Sequence) => [seq.domain, seq.pattern], { unique: true })
8
+ @Index('ix_sequence_1', (seq: Sequence) => [seq.pattern])
9
+ @Index('ix_sequence_2', (seq: Sequence) => [seq.expiresAt])
10
+ @ObjectType()
11
+ export class Sequence {
12
+ @PrimaryGeneratedColumn('uuid')
13
+ @Field(type => ID)
14
+ readonly id: string
15
+
16
+ @ManyToOne(type => Domain)
17
+ @Field(type => Domain)
18
+ domain: Domain
19
+
20
+ @RelationId((sequence: Sequence) => sequence.domain)
21
+ domainId?: string
22
+
23
+ @Column()
24
+ @Field()
25
+ pattern: string
26
+
27
+ @Column({
28
+ type: 'smallint'
29
+ })
30
+ @Field()
31
+ seq: number
32
+
33
+ //// to differentiate between normal pattern sequence VS label counter ////
34
+ @Column({ nullable: true })
35
+ @Field()
36
+ type: string
37
+
38
+ @Column({ nullable: true })
39
+ @Field()
40
+ expiresAt: Date
41
+
42
+ @CreateDateColumn()
43
+ @Field({ nullable: true })
44
+ createdAt: Date
45
+
46
+ @UpdateDateColumn()
47
+ @Field({ nullable: true })
48
+ updatedAt: Date
49
+ }