Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align with legacy db #31

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { ApiModule } from './services/api/api.module';
import { AuthModule } from './services/auth/auth.module';
import { HiveChainModule } from './repositories/hive-chain/hive-chain.module';
import { HiveAccountModule } from './repositories/hive-account/hive-account.module';
import { LinkedAccountModule } from './repositories/linked-accounts/linked-account.module';
import { SessionModule } from './repositories/session/session.module';
import { UploadModule } from './repositories/upload/upload.module';
import { UserAccountModule } from './repositories/userAccount/user-account.module';
Expand Down Expand Up @@ -64,7 +63,6 @@ const mongoUrl = process.env.CORE_MONGODB_URL || 'mongodb://mongo:27017';
IpfsModule,
UploadingModule,
HiveAccountModule,
LinkedAccountModule,
SessionModule,
UploadModule,
EmailModule,
Expand Down
10 changes: 5 additions & 5 deletions src/repositories/hive-account/hive-account.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { HiveAccount, HiveAccountSchema } from './schemas/hive-account.schema';
import { HiveAccountRepository } from './hive-account.repository';
import { LegacyHiveAccountSchema } from './schemas/hive-account.schema';
import { LegacyHiveAccountRepository } from './hive-account.repository';

@Module({
imports: [
MongooseModule.forFeature(
[{ name: HiveAccount.name, schema: HiveAccountSchema }],
[{ name: 'hiveaccounts', schema: LegacyHiveAccountSchema }],
'threespeak',
),
],
controllers: [],
providers: [HiveAccountRepository],
exports: [HiveAccountRepository],
providers: [LegacyHiveAccountRepository],
exports: [LegacyHiveAccountRepository],
})
export class HiveAccountModule {}
72 changes: 72 additions & 0 deletions src/repositories/hive-account/hive-account.repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'dotenv/config'
import { Test } from '@nestjs/testing';
import { MongooseModule } from '@nestjs/mongoose';
import { UserModule } from '../../repositories/user/user.module';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { INestApplication, Module } from '@nestjs/common';
import { TestingModule } from '@nestjs/testing';
import crypto from 'crypto';
import { LegacyUserSchema } from '../user/schemas/user.schema';
import { HiveAccountModule } from './hive-account.module';
import { LegacyHiveAccountRepository } from './hive-account.repository';
import { LegacyHiveAccountSchema } from './schemas/hive-account.schema';
import { LegacyUserRepository } from '../user/user.repository';

describe('Legacy hive account', () => {
let app: INestApplication
let mongod: MongoMemoryServer;
let hiveAccountRepository: LegacyHiveAccountRepository;
let legacyUserRepository: LegacyUserRepository;

beforeEach(async () => {
mongod = await MongoMemoryServer.create()
const uri: string = mongod.getUri()

process.env.JWT_PRIVATE_KEY = crypto.randomBytes(64).toString('hex');
process.env.DELEGATED_ACCOUNT = 'threespeak';
process.env.ACCOUNT_CREATOR = 'threespeak';

@Module({
imports: [
MongooseModule.forRoot(uri, {
ssl: false,
authSource: 'threespeak',
readPreference: 'primary',
connectionName: 'threespeak',
dbName: 'threespeak',
autoIndex: true,
}),
MongooseModule.forFeature([{ name: 'hiveaccounts', schema: LegacyHiveAccountSchema }], 'threespeak'),
MongooseModule.forFeature([{ name: 'users', schema: LegacyUserSchema }], 'threespeak'),
HiveAccountModule,
UserModule
],
controllers: [],
providers: [LegacyHiveAccountRepository, LegacyUserRepository]
})
class TestModule {}

let moduleRef: TestingModule;

moduleRef = await Test.createTestingModule({
imports: [TestModule],
}).compile()
hiveAccountRepository = moduleRef.get<LegacyHiveAccountRepository>(LegacyHiveAccountRepository);
legacyUserRepository = moduleRef.get<LegacyUserRepository>(LegacyUserRepository);
app = moduleRef.createNestApplication();
await app.init()
})

afterEach(async () => {
await app.close();
await mongod.stop();
});

describe('User repository', () => {
it(`Successfully creates a new user`, async () => {
await legacyUserRepository.createNewSubUser({ sub: 'singleton/did/check', user_id: 'example' })
const result = await legacyUserRepository.findOneBySub('singleton/did/check')
expect(result).toBeTruthy()
});
})
});
38 changes: 18 additions & 20 deletions src/repositories/hive-account/hive-account.repository.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { HiveAccount } from './schemas/hive-account.schema';
import { ObjectId } from 'mongodb';
import { LegacyHiveAccount } from './schemas/hive-account.schema';

@Injectable()
export class HiveAccountRepository {
readonly #logger = new Logger(HiveAccountRepository.name);
export class LegacyHiveAccountRepository {
readonly #logger = new Logger(LegacyHiveAccountRepository.name);

constructor(
@InjectModel(HiveAccount.name, 'threespeak') private hiveAccountModel: Model<HiveAccount>,
@InjectModel('hiveaccounts', 'threespeak') private hiveAccountModel: Model<LegacyHiveAccount>,
) {}

async findOneByOwnerIdAndHiveAccountName({
user_id,
account,
}: {
user_id: string | ObjectId;
account: string;
}): Promise<HiveAccount | null> {
const acelaUser = await this.hiveAccountModel.findOne({ user_id, account });
async findOneByOwnerIdAndHiveAccountName(
query: Pick<LegacyHiveAccount, 'user_id' | 'account'>,
): Promise<LegacyHiveAccount | null> {
const acelaUser = await this.hiveAccountModel.findOne(query);
this.#logger.log(acelaUser);

return acelaUser;
}

async findOneByOwnerId({ user_id }: { user_id: string | ObjectId }): Promise<HiveAccount | null> {
const acelaUser = await this.hiveAccountModel.findOne({ user_id });
async findOneByOwnerId(
query: Pick<LegacyHiveAccount, 'user_id'>,
): Promise<LegacyHiveAccount | null> {
const acelaUser = await this.hiveAccountModel.findOne(query);
this.#logger.log(acelaUser);

return acelaUser;
Expand All @@ -45,10 +42,11 @@ export class HiveAccountRepository {
});
}

async insertCreated(username: string, created_by: string) {
return await this.hiveAccountModel.create({
account: username,
user_id: created_by,
});
async deleteOne(query: Pick<LegacyHiveAccount, 'user_id' | 'account'>) {
return this.hiveAccountModel.deleteOne(query);
}

async insertCreated(query: Pick<LegacyHiveAccount, 'user_id' | 'account'>) {
return await this.hiveAccountModel.create(query);
}
}
6 changes: 3 additions & 3 deletions src/repositories/hive-account/schemas/hive-account.schema.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, HydratedDocument, Types } from 'mongoose';

export type HiveAccountDocument = HydratedDocument<HiveAccount>;
export type HiveAccountDocument = HydratedDocument<LegacyHiveAccount>;

@Schema()
export class HiveAccount extends Document {
export class LegacyHiveAccount extends Document {
@Prop({ type: String, required: true })
account: string;

@Prop({ type: Types.ObjectId, required: true })
user_id: Types.ObjectId;
}

export const HiveAccountSchema = SchemaFactory.createForClass(HiveAccount);
export const LegacyHiveAccountSchema = SchemaFactory.createForClass(LegacyHiveAccount);
17 changes: 0 additions & 17 deletions src/repositories/linked-accounts/linked-account.module.ts

This file was deleted.

59 changes: 0 additions & 59 deletions src/repositories/linked-accounts/linked-account.repository.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/repositories/linked-accounts/schemas/linked-account.schema.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/repositories/session/session.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export class SessionRepository {
constructor(@InjectModel('auth_sessions', 'acela-core') private sessionModel: Model<Session>) {}

async insertOne({ id, type, sub }: { id: string; type?: string; sub?: string }) {
return await this.sessionModel.create({ id, type, sub });
return this.sessionModel.create({ id, type, sub });
}

async findOneBySub(sub: string) {
return await this.sessionModel.findOne({ sub });
return this.sessionModel.findOne({ sub });
}
}
9 changes: 3 additions & 6 deletions src/repositories/upload/upload.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { FilterQuery, Model, QueryOptions, Types, UpdateQuery } from 'mongoose';
import { Upload } from './schemas/upload.schema';
import { User } from '../../services/auth/auth.types';

@Injectable()
export class UploadRepository {
Expand Down Expand Up @@ -57,11 +58,7 @@ export class UploadRepository {
id: string,
cid: string,
video_id: string,
user: {
sub: string;
username: string;
id?: string;
},
user: User,
): Promise<Upload> {
return await this.uploadModel.create({
id,
Expand All @@ -72,7 +69,7 @@ export class UploadRepository {
ipfs_status: 'done',
cid: cid,
type: 'thumbnail',
created_by: user.sub,
created_by: user.user_id,
});
}

Expand Down
34 changes: 17 additions & 17 deletions src/repositories/user/schemas/user.schema.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, HydratedDocument, Types } from 'mongoose';
import { HiveAccount } from '../../hive-account/schemas/hive-account.schema';
import { v4 as uuid } from 'uuid';
import { HydratedDocument, Types } from 'mongoose';
import { LegacyHiveAccount } from '../../hive-account/schemas/hive-account.schema';

export type UserDocument = HydratedDocument<User>;
export type LegacyUserDocument = HydratedDocument<LegacyUser>;

@Schema()
export class User extends Document {
@Prop({ type: String, unique: true, default: () => uuid() })
user_id: string;
export class LegacyUser {
// same as userAccount.username
@Prop({ type: String, required: true })
user_id!: string;

@Prop({ type: Boolean, required: true, default: false })
banned: boolean;
@Prop({ type: String, unique: true })
sub?: string;

@Prop({ type: String, required: true, unique: true })
email: string;
@Prop({ type: Boolean, required: true, default: false })
banned?: boolean;

@Prop({ type: Types.ObjectId, ref: HiveAccount.name }) // Assuming 'Identity' is another schema
last_identity: Types.ObjectId;
@Prop({ type: String })
email?: string;

@Prop()
display_name: string;
@Prop({ type: Types.ObjectId, ref: LegacyHiveAccount.name })
last_identity?: Types.ObjectId;

@Prop()
self_deleted: boolean;
self_deleted?: boolean;
}

export const UserSchema = SchemaFactory.createForClass(User);
export const LegacyUserSchema = SchemaFactory.createForClass(LegacyUser);
Loading