mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
use internal EntityRepository
This commit is contained in:
parent
410854054b
commit
2ad92ed3d2
@ -6,7 +6,7 @@ module.exports = {
|
||||
collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/seeds/**', '!build/**'],
|
||||
coverageThreshold: {
|
||||
global: {
|
||||
lines: 72,
|
||||
lines: 70,
|
||||
},
|
||||
},
|
||||
setupFiles: ['<rootDir>/test/testSetup.ts'],
|
||||
|
||||
@ -1,14 +1,10 @@
|
||||
import 'reflect-metadata'
|
||||
import { CommunityDraft } from '@/graphql/input/CommunityDraft'
|
||||
import {
|
||||
create as createCommunity,
|
||||
getAllTopics,
|
||||
iotaTopicFromCommunityUUID,
|
||||
isExist,
|
||||
} from './Community'
|
||||
import { create as createCommunity, getAllTopics, isExist } from './Community'
|
||||
import { TestDB } from '@test/TestDB'
|
||||
import { getDataSource } from '@/typeorm/DataSource'
|
||||
import { Community } from '@entity/Community'
|
||||
import { iotaTopicFromCommunityUUID } from '@/utils/typeConverter'
|
||||
|
||||
jest.mock('@typeorm/DataSource', () => ({
|
||||
getDataSource: () => TestDB.instance.dbConnect,
|
||||
@ -61,7 +57,7 @@ describe('controller/Community', () => {
|
||||
})
|
||||
|
||||
it('createdAt with ms precision', async () => {
|
||||
const list = await getDataSource().manager.findOne(Community, { where: { foreign: false } })
|
||||
const list = await Community.findOne({ where: { foreign: false } })
|
||||
expect(list).toMatchObject({
|
||||
createdAt: new Date('2022-05-01T17:00:12.128Z'),
|
||||
})
|
||||
|
||||
@ -1,26 +1,18 @@
|
||||
import { CommunityDraft } from '@/graphql/input/CommunityDraft'
|
||||
import { uuid4ToBuffer } from '@/utils/typeConverter'
|
||||
import { iotaTopicFromCommunityUUID } from '@/utils/typeConverter'
|
||||
import { Community } from '@entity/Community'
|
||||
import { getDataSource } from '@typeorm/DataSource'
|
||||
import { crypto_generichash as cryptoHash } from 'sodium-native'
|
||||
|
||||
export const iotaTopicFromCommunityUUID = (communityUUID: string): string => {
|
||||
const hash = Buffer.alloc(32)
|
||||
cryptoHash(hash, uuid4ToBuffer(communityUUID))
|
||||
return hash.toString('hex')
|
||||
}
|
||||
|
||||
export const isExist = async (community: CommunityDraft | string): Promise<boolean> => {
|
||||
const iotaTopic =
|
||||
community instanceof CommunityDraft ? iotaTopicFromCommunityUUID(community.uuid) : community
|
||||
const result = await getDataSource().manager.find(Community, {
|
||||
const result = await Community.find({
|
||||
where: { iotaTopic },
|
||||
})
|
||||
return result.length > 0
|
||||
}
|
||||
|
||||
export const create = (community: CommunityDraft, topic?: string): Community => {
|
||||
const communityEntity = new Community()
|
||||
const communityEntity = Community.create()
|
||||
communityEntity.iotaTopic = topic ?? iotaTopicFromCommunityUUID(community.uuid)
|
||||
communityEntity.createdAt = new Date(community.createdAt)
|
||||
communityEntity.foreign = community.foreign
|
||||
@ -31,6 +23,6 @@ export const create = (community: CommunityDraft, topic?: string): Community =>
|
||||
}
|
||||
|
||||
export const getAllTopics = async (): Promise<string[]> => {
|
||||
const communities = await getDataSource().manager.find(Community, { select: { iotaTopic: true } })
|
||||
const communities = await Community.find({ select: { iotaTopic: true } })
|
||||
return communities.map((community) => community.iotaTopic)
|
||||
}
|
||||
|
||||
@ -1,18 +1,13 @@
|
||||
import { Resolver, Query, Arg, Mutation } from 'type-graphql'
|
||||
import { Resolver, Arg, Mutation } from 'type-graphql'
|
||||
|
||||
import { CommunityDraft } from '@input/CommunityDraft'
|
||||
|
||||
import { getDataSource } from '@typeorm/DataSource'
|
||||
|
||||
import { TransactionResult } from '../model/TransactionResult'
|
||||
import { TransactionError } from '../model/TransactionError'
|
||||
import {
|
||||
create as createCommunity,
|
||||
iotaTopicFromCommunityUUID,
|
||||
isExist,
|
||||
} from '@/controller/Community'
|
||||
import { create as createCommunity, isExist } from '@/controller/Community'
|
||||
import { TransactionErrorType } from '../enum/TransactionErrorType'
|
||||
import { logger } from '@/server/logger'
|
||||
import { iotaTopicFromCommunityUUID } from '@/utils/typeConverter'
|
||||
|
||||
@Resolver()
|
||||
export class CommunityResolver {
|
||||
@ -38,7 +33,7 @@ export class CommunityResolver {
|
||||
// TODO: CommunityRoot Transaction for blockchain
|
||||
}
|
||||
try {
|
||||
await getDataSource().manager.save(community)
|
||||
await community.save()
|
||||
result = new TransactionResult()
|
||||
} catch (err) {
|
||||
logger.error('error saving new community into db: %s', err)
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
import { crypto_generichash as cryptoHash } from 'sodium-native'
|
||||
|
||||
export const uuid4ToBuffer = (uuid: string): Buffer => {
|
||||
// Remove dashes from the UUIDv4 string
|
||||
const cleanedUUID = uuid.replace(/-/g, '')
|
||||
@ -7,3 +9,9 @@ export const uuid4ToBuffer = (uuid: string): Buffer => {
|
||||
|
||||
return buffer
|
||||
}
|
||||
|
||||
export const iotaTopicFromCommunityUUID = (communityUUID: string): string => {
|
||||
const hash = Buffer.alloc(32)
|
||||
cryptoHash(hash, uuid4ToBuffer(communityUUID))
|
||||
return hash.toString('hex')
|
||||
}
|
||||
|
||||
@ -1,4 +1,12 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, OneToMany } from 'typeorm'
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
OneToMany,
|
||||
BaseEntity,
|
||||
} from 'typeorm'
|
||||
import { User } from '../User'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
import { ConfirmedTransaction } from '../ConfirmedTransaction'
|
||||
@ -7,7 +15,7 @@ import { Decimal } from 'decimal.js-light'
|
||||
import { AccountCommunity } from '../AccountCommunity'
|
||||
|
||||
@Entity('accounts')
|
||||
export class Account {
|
||||
export class Account extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true })
|
||||
id: number
|
||||
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm'
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, BaseEntity } from 'typeorm'
|
||||
|
||||
import { Account } from '../Account'
|
||||
import { Community } from '../Community'
|
||||
|
||||
@Entity('accounts_communities')
|
||||
export class AccountCommunity {
|
||||
export class AccountCommunity extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true })
|
||||
id: number
|
||||
|
||||
|
||||
@ -1,10 +1,18 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, JoinColumn, OneToOne, OneToMany } from 'typeorm'
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
JoinColumn,
|
||||
OneToOne,
|
||||
OneToMany,
|
||||
BaseEntity,
|
||||
} from 'typeorm'
|
||||
import { Account } from '../Account'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
import { AccountCommunity } from '../AccountCommunity'
|
||||
|
||||
@Entity('communities')
|
||||
export class Community {
|
||||
export class Community extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true })
|
||||
id: number
|
||||
|
||||
|
||||
@ -1,4 +1,12 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, OneToOne } from 'typeorm'
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
OneToOne,
|
||||
BaseEntity,
|
||||
} from 'typeorm'
|
||||
import { Decimal } from 'decimal.js-light'
|
||||
|
||||
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
|
||||
@ -6,7 +14,7 @@ import { Account } from '../Account'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
|
||||
@Entity('confirmed_transactions')
|
||||
export class ConfirmedTransaction {
|
||||
export class ConfirmedTransaction extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true, type: 'bigint' })
|
||||
id: number
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
|
||||
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from 'typeorm'
|
||||
|
||||
@Entity('invalid_transactions')
|
||||
export class InvalidTransaction {
|
||||
export class InvalidTransaction extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true, type: 'bigint' })
|
||||
id: number
|
||||
|
||||
|
||||
@ -1,4 +1,12 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToOne, JoinColumn } from 'typeorm'
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
ManyToOne,
|
||||
OneToOne,
|
||||
JoinColumn,
|
||||
BaseEntity,
|
||||
} from 'typeorm'
|
||||
import { Decimal } from 'decimal.js-light'
|
||||
|
||||
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
|
||||
@ -7,7 +15,7 @@ import { Community } from '../Community'
|
||||
import { ConfirmedTransaction } from '../ConfirmedTransaction'
|
||||
|
||||
@Entity('transaction_recipes')
|
||||
export class TransactionRecipe {
|
||||
export class TransactionRecipe extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true, type: 'bigint' })
|
||||
id: number
|
||||
|
||||
|
||||
@ -1,4 +1,12 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, OneToMany } from 'typeorm'
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
OneToMany,
|
||||
BaseEntity,
|
||||
} from 'typeorm'
|
||||
import { User } from '../User'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
import { ConfirmedTransaction } from '../ConfirmedTransaction'
|
||||
@ -7,7 +15,7 @@ import { Decimal } from 'decimal.js-light'
|
||||
import { AccountCommunity } from '../AccountCommunity'
|
||||
|
||||
@Entity('accounts')
|
||||
export class Account {
|
||||
export class Account extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true })
|
||||
id: number
|
||||
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm'
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, BaseEntity } from 'typeorm'
|
||||
|
||||
import { Account } from '../Account'
|
||||
import { Community } from '../Community'
|
||||
|
||||
@Entity('accounts_communities')
|
||||
export class AccountCommunity {
|
||||
export class AccountCommunity extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true })
|
||||
id: number
|
||||
|
||||
|
||||
@ -1,10 +1,18 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, JoinColumn, OneToOne, OneToMany } from 'typeorm'
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
JoinColumn,
|
||||
OneToOne,
|
||||
OneToMany,
|
||||
BaseEntity,
|
||||
} from 'typeorm'
|
||||
import { Account } from '../Account'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
import { AccountCommunity } from '../AccountCommunity'
|
||||
|
||||
@Entity('communities')
|
||||
export class Community {
|
||||
export class Community extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true })
|
||||
id: number
|
||||
|
||||
|
||||
@ -1,4 +1,12 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, OneToOne } from 'typeorm'
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
OneToOne,
|
||||
BaseEntity,
|
||||
} from 'typeorm'
|
||||
import { Decimal } from 'decimal.js-light'
|
||||
|
||||
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
|
||||
@ -6,7 +14,7 @@ import { Account } from '../Account'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
|
||||
@Entity('confirmed_transactions')
|
||||
export class ConfirmedTransaction {
|
||||
export class ConfirmedTransaction extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true, type: 'bigint' })
|
||||
id: number
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user