Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
luixo committed Oct 24, 2022
1 parent a084599 commit 6a22892
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 74 deletions.
2 changes: 0 additions & 2 deletions backend/src/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { AuthModule } from './auth/auth.module';
import validate from '../config/validate';

import { ApiKeysModule } from './keys/apiKeys.module';
import { IndexerService } from './indexer.service';
import { EmailModule } from './email/email.module';

@Module({
Expand All @@ -26,7 +25,6 @@ import { EmailModule } from './email/email.module';
EmailModule,
ExplorerModule,
],
providers: [IndexerService],
exports: [EmailModule],
})
export class CoreModule {}
8 changes: 8 additions & 0 deletions backend/src/core/explorer/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ export const BalanceChangesInputSchemas = Joi.object<
receiptId: Joi.string(),
accountIds: Joi.array().items(Joi.string()),
});

export const GetTransactionsSchema = Joi.object<
Api.Input<'/explorer/getTransactions'>,
true
>({
contracts: Joi.array().items(Joi.string()),
net: netSchema,
});
15 changes: 14 additions & 1 deletion backend/src/core/explorer/explorer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import {
ActivityInputSchemas,
TransactionInputSchemas,
BalanceChangesInputSchemas,
GetTransactionsSchema,
} from './dto';
import { IndexerService } from './indexer.service';

@Controller('explorer')
export class ExplorerController {
constructor(private readonly explorerService: ExplorerService) {}
constructor(
private readonly explorerService: ExplorerService,
private readonly indexerService: IndexerService,
) {}

@Post('activity')
@UsePipes(new JoiValidationPipe(ActivityInputSchemas))
Expand Down Expand Up @@ -46,4 +51,12 @@ export class ExplorerController {
): Promise<Api.Output<'/explorer/balanceChanges'>> {
return this.explorerService.fetchBalanceChanges(net, receiptId, accountIds);
}

@Post('getTransactions')
@UsePipes(new JoiValidationPipe(GetTransactionsSchema))
async getTransactions(
@Body() { contracts, net }: Api.Input<'/explorer/getTransactions'>,
): Promise<Api.Output<'/explorer/getTransactions'>> {
return this.indexerService.fetchRecentTransactions(contracts, net);
}
}
5 changes: 3 additions & 2 deletions backend/src/core/explorer/explorer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common';
import { NearRpcService } from '../near-rpc/near-rpc.service';
import { ExplorerController } from './explorer.controller';
import { ExplorerService } from './explorer.service';
import { IndexerService } from './indexer.service';

@Module({
providers: [ExplorerService, NearRpcService],
controllers: [ExplorerController],
providers: [ExplorerService, IndexerService, NearRpcService],
controllers: [ExplorerController, IndexerService],
exports: [ExplorerService],
})
export class ExplorerModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const DS_INDEXER_TESTNET = 'DS_INDEXER_TESTNET';

import { Sequelize, QueryTypes } from 'sequelize';
import { Net } from '@pc/database/clients/core';
import { AppConfig } from '../config/validate';
import { AppConfig } from '../../config/validate';
import { Explorer } from '@pc/common/types';

@Injectable()
Expand Down
11 changes: 0 additions & 11 deletions backend/src/core/projects/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import * as Joi from 'joi';

const projectNameSchema = Joi.string().required().max(50);

const netSchema = Joi.alternatives('MAINNET', 'TESTNET');

// create project
export const CreateProjectSchema = Joi.object<
Api.Input<'/projects/create'>,
Expand Down Expand Up @@ -115,12 +113,3 @@ export const DeleteKeySchema = Joi.object<
>({
slug: Joi.string().required(),
});

// rotate transactions
export const GetTransactionsSchema = Joi.object<
Api.Input<'/projects/getTransactions'>,
true
>({
contracts: Joi.array().items(Joi.string()),
net: netSchema,
});
16 changes: 1 addition & 15 deletions backend/src/core/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@ import {
GetEnvironmentsSchema,
GetKeysSchema,
GetProjectDetailsSchema,
GetTransactionsSchema,
RemoveContractSchema,
RotateKeySchema,
} from './dto';
import { JoiValidationPipe } from 'src/pipes/JoiValidationPipe';
import { IndexerService } from '../indexer.service';
import { Api } from '@pc/common/types';

@Controller('projects')
export class ProjectsController {
constructor(
private readonly projectsService: ProjectsService,
private readonly indexerService: IndexerService,
) {}
constructor(private readonly projectsService: ProjectsService) {}

@Post('create')
@UseGuards(BearerAuthGuard)
Expand Down Expand Up @@ -257,15 +252,6 @@ export class ProjectsController {
throw mapError(e);
}
}

@Post('getTransactions')
@UseGuards(BearerAuthGuard)
@UsePipes(new JoiValidationPipe(GetTransactionsSchema))
async getTransactions(
@Body() { contracts, net }: Api.Input<'/projects/getTransactions'>,
): Promise<Api.Output<'/projects/getTransactions'>> {
return this.indexerService.fetchRecentTransactions(contracts, net);
}
}

function mapError(e: Error) {
Expand Down
10 changes: 1 addition & 9 deletions backend/src/core/projects/projects.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Module } from '@nestjs/common';
import { IndexerService } from '../indexer.service';
import { ApiKeysModule } from '../keys/apiKeys.module';
import { NearRpcService } from '@/src/core/near-rpc/near-rpc.service';
import { PrismaService } from '../prisma.service';
Expand All @@ -13,19 +12,12 @@ import { UsersModule } from '../users/users.module';
providers: [
ProjectsService,
PrismaService,
IndexerService,
PermissionsService,
ReadonlyService,
NearRpcService,
],
controllers: [ProjectsController],
imports: [
PrismaService,
ApiKeysModule,
IndexerService,
NearRpcService,
UsersModule,
],
imports: [PrismaService, ApiKeysModule, NearRpcService, UsersModule],
exports: [ProjectsService, PermissionsService, ReadonlyService],
})
export class ProjectsModule {}
16 changes: 7 additions & 9 deletions backend/src/core/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,17 +746,15 @@ export class ProjectsService {
},
});

return projects.map((p) => {
const isPersonal = !!p.org.personalForUserId;
return {
...p,
return projects.map(
({ org: { personalForUserId, ...org }, ...project }) => ({
...project,
org: {
name: isPersonal ? undefined : p.org.name,
slug: p.org.slug,
isPersonal,
...org,
isPersonal: !!personalForUserId,
},
};
});
}),
);
}

async getEnvironments(
Expand Down
4 changes: 1 addition & 3 deletions backend/src/core/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,7 @@ export class UsersService implements OnModuleInit {
);
}

return {
org,
};
return org;
}

async removeFromOrg(
Expand Down
29 changes: 10 additions & 19 deletions common/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ type Api = {
output: Explorer.Transaction;
error: unknown;
};
'/explorer/getTransactions': {
input: { net: Net; contracts: string[] };
output: (Explorer.TransactionOld & { sourceContract: string })[];
error: unknown;
};

'/projects/create': {
input: {
Expand Down Expand Up @@ -77,20 +82,15 @@ type Api = {
CoreDatabase.Project,
'id' | 'name' | 'slug' | 'tutorial' | 'active'
> & {
// TODO: remove mapping, leave org selection as is
org: Pick<CoreDatabase.Org, 'slug'> &
Partial<Pick<CoreDatabase.Org, 'name'>> & {
isPersonal: boolean;
};
org: Pick<CoreDatabase.Org, 'slug' | 'name'> & {
isPersonal: boolean;
};
})[];
error: unknown;
};
'/projects/getEnvironments': {
input: { project: string };
output: (Pick<CoreDatabase.Environment, 'subId' | 'net' | 'name'> & {
// TODO: do we actually need full contract rows?
contracts: CoreDatabase.Contract[];
})[];
output: Pick<CoreDatabase.Environment, 'subId' | 'net' | 'name'>[];
error: unknown;
};
'/projects/getKeys': {
Expand Down Expand Up @@ -122,12 +122,6 @@ type Api = {
output: void;
error: unknown;
};
'/projects/getTransactions': {
input: { contracts: string[]; net: Net };
// TODO: why do we have a separate call in `projects` namespace?
output: (Explorer.TransactionOld & { sourceContract: string })[];
error: unknown;
};

'/users/getAccountDetails': {
input: void;
Expand Down Expand Up @@ -166,10 +160,7 @@ type Api = {
};
'/users/acceptOrgInvite': {
input: { token: string };
output: {
// TODO: Why do we need `org` wrapper?
org: Pick<CoreDatabase.Org, 'name' | 'slug'>;
};
output: Pick<CoreDatabase.Org, 'name' | 'slug'>;
error: unknown;
};
'/users/listOrgMembers': {
Expand Down
2 changes: 1 addition & 1 deletion frontend/hooks/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function useProjectGroups() {
projectGroups: projects
? Object.entries(
projects.reduce<Record<string, Api.Output<'/projects/list'>>>((acc, project) => {
const orgName = project.org.isPersonal ? 'Personal' : project.org.name || 'unknown';
const orgName = project.org.isPersonal ? 'Personal' : project.org.name;
if (!acc[orgName]) {
acc[orgName] = [];
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/modules/contracts/hooks/recent-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function useRecentTransactions(contract: string | undefined, net: Net | u
// separate key

const { data: transactions, error } = useSWR(
identity && contract && net ? ['/projects/getTransactions' as const, contract, net, identity.uid] : null,
identity && contract && net ? ['/explorer/getTransactions' as const, contract, net, identity.uid] : null,
(key, contracts, net) => {
return authenticatedPost(key, {
contracts: contracts.split(','),
Expand Down

0 comments on commit 6a22892

Please sign in to comment.