Skip to content

Commit

Permalink
fix misc controller issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tmthecoder committed Nov 4, 2024
1 parent b789b16 commit 71f731d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ export class FileConfigController
async createConfig(
request: FileConfigProto.CreateFileServiceConfigRequest,
): Promise<FileConfigProto.FileServiceConfig> {
const configId = request.projectId.toString();
const configId = request.projectId;

const existingConfig =
await this.fileServiceConfigService.getConfig(configId);
if (existingConfig) {
try {
const existingConfig =
await this.fileServiceConfigService.getConfig(configId);
if (existingConfig) {
throw new RpcException({
code: status.ALREADY_EXISTS,
message: 'Config already exists',
});
}
} catch (error) {
throw new RpcException({
code: status.ALREADY_EXISTS,
message: 'Config already exists',
Expand All @@ -40,9 +47,7 @@ export class FileConfigController
async getConfig(
request: FileConfigProto.GetFileServiceConfigRequest,
): Promise<FileConfigProto.FileServiceConfig> {
const config = await this.fileServiceConfigService.getConfig(
request.id.toString(),
);
const config = await this.fileServiceConfigService.getConfig(request.id);
if (!config) {
throw new RpcException({
code: status.NOT_FOUND,
Expand All @@ -62,7 +67,7 @@ export class FileConfigController
): Promise<FileConfigProto.FileServiceConfig> {
try {
const config = await this.fileServiceConfigService.updateConfig(
request.id.toString(),
request.id,
request,
);

Expand Down Expand Up @@ -93,7 +98,7 @@ export class FileConfigController
): Promise<FileConfigProto.FileServiceConfig> {
try {
const config = await this.fileServiceConfigService.deleteConfig(
request.id.toString(),
request.id,
);
return {
id: config.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class FileServiceConfigService {
data: {
Project: {
connect: {
id: configData.projectId,
id: Number(configData.projectId),
},
},
},
Expand All @@ -26,7 +26,7 @@ export class FileServiceConfigService {
});
}

async getConfig(configId: string): Promise<FileServiceConfig | null> {
async getConfig(configId: number): Promise<FileServiceConfig | null> {
return this.prisma.fileServiceConfig.findUnique({
where: {
id: Number(configId),
Expand All @@ -40,7 +40,7 @@ export class FileServiceConfigService {
}

async updateConfig(
configId: string,
configId: number,
configData: FileConfigProto.UpdateFileServiceConfigRequest,
): Promise<FileServiceConfig> {
return this.prisma.fileServiceConfig.update({
Expand All @@ -56,7 +56,7 @@ export class FileServiceConfigService {
},
Project: {
connect: {
id: configData.id,
id: Number(configData.id),
},
},
FileServiceFile: {
Expand All @@ -80,7 +80,7 @@ export class FileServiceConfigService {
});
}

async deleteConfig(configId: string): Promise<FileServiceConfig> {
async deleteConfig(configId: number): Promise<FileServiceConfig> {
return this.prisma.fileServiceConfig.delete({
where: {
id: Number(configId),
Expand Down
96 changes: 69 additions & 27 deletions packages/db-service/test/file_config.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import { AppModule } from '../src/app.module';
import * as ProtoLoader from '@grpc/proto-loader';
import * as GRPC from '@grpc/grpc-js';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { FileConfigProto, FileConfigProtoFile } from 'juno-proto';
import {
FileConfigProto,
FileConfigProtoFile,
ResetProto,
ResetProtoFile,
} from 'juno-proto';

let app: INestMicroservice;
let createdConfigId: string;
let createdConfigId: number;

jest.setTimeout(15000);

Expand All @@ -19,8 +24,11 @@ async function initApp() {
const app = moduleFixture.createNestMicroservice<MicroserviceOptions>({
transport: Transport.GRPC,
options: {
package: [FileConfigProto.JUNO_FILE_SERVICE_CONFIG_PACKAGE_NAME],
protoPath: [FileConfigProtoFile],
package: [
FileConfigProto.JUNO_FILE_SERVICE_CONFIG_PACKAGE_NAME,
ResetProto.JUNO_RESET_DB_PACKAGE_NAME,
],
protoPath: [FileConfigProtoFile, ResetProtoFile],
url: process.env.DB_SERVICE_ADDR,
},
});
Expand All @@ -32,12 +40,47 @@ async function initApp() {

beforeAll(async () => {
app = await initApp();
const proto = ProtoLoader.loadSync([ResetProtoFile]) as any;

const protoGRPC = GRPC.loadPackageDefinition(proto) as any;

const resetClient = new protoGRPC.juno.reset_db.DatabaseReset(
process.env.DB_SERVICE_ADDR,
GRPC.credentials.createInsecure(),
);

await new Promise((resolve) => {
resetClient.resetDb({}, () => {
resolve(0);
});
});

app.close();
});

beforeEach(async () => {
app = await initApp();
});

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

function tryCreateConfig(configClient: any) {
return new Promise<void>((resolve) => {
configClient.createConfig(
{
projectId: 0,
buckets: [],
files: [],
},
() => {
resolve();
},
);
});
}

describe('File Service Config Tests', () => {
let configClient: any;

Expand All @@ -56,11 +99,9 @@ describe('File Service Config Tests', () => {
(resolve, reject) => {
configClient.createConfig(
{
config: {
projectId: '1',
buckets: [],
files: [],
},
projectId: 0,
buckets: [],
files: [],
},
(err, res) => {
if (err) {
Expand All @@ -82,11 +123,9 @@ describe('File Service Config Tests', () => {
await new Promise((resolve, reject) => {
configClient.createConfig(
{
config: {
projectId: '1',
buckets: [],
files: [],
},
projectId: 0,
buckets: [],
files: [],
},
(err, res) => {
if (err) {
Expand All @@ -103,6 +142,7 @@ describe('File Service Config Tests', () => {
});

it('deletes a config', async () => {
await tryCreateConfig(configClient);
const deleteResponse = await new Promise((resolve, reject) => {
configClient.deleteConfig({ id: createdConfigId }, (err, res) => {
if (err) {
Expand All @@ -119,29 +159,30 @@ describe('File Service Config Tests', () => {
it('deletes a nonexistent config', async () => {
try {
await new Promise((resolve, reject) => {
configClient.deleteConfig({ id: 'nonexistent-id' }, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
configClient.deleteConfig(
{ id: createdConfigId + 1000000 },
(err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
},
);
});
} catch (err) {
expect(err).toBeDefined();
}
});

it('updates a config', async () => {
await tryCreateConfig(configClient);
const updateResponse = await new Promise((resolve, reject) => {
configClient.updateConfig(
{
id: createdConfigId,
config: {
projectId: '1',
buckets: [],
files: [],
},
buckets: [],
files: [],
},
(err, res) => {
if (err) {
Expand All @@ -159,7 +200,7 @@ describe('File Service Config Tests', () => {
it('reading a nonexistent config', async () => {
try {
await new Promise((resolve, reject) => {
configClient.getConfig({ id: 'nonexistent-id' }, (err, res) => {
configClient.getConfig({ id: createdConfigId + 10000 }, (err, res) => {
if (err) {
reject(err);
} else {
Expand All @@ -173,6 +214,7 @@ describe('File Service Config Tests', () => {
});

it('reading a config', async () => {
await tryCreateConfig(configClient);
const readResponse: FileConfigProto.FileServiceConfig = await new Promise(
(resolve, reject) => {
configClient.getConfig({ id: createdConfigId }, (err, res) => {
Expand Down

0 comments on commit 71f731d

Please sign in to comment.