Save a GrpcRequest after protofile selection (#2785)

This commit is contained in:
Opender Singh 2020-10-29 11:23:07 +13:00 committed by GitHub
parent 33ef367f3f
commit d1e1da53e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 12 deletions

View File

@ -6,6 +6,7 @@ describe('init()', () => {
it('contains all required fields', async () => {
expect(models.grpcRequestMeta.init()).toEqual({
pinned: false,
lastActive: 0,
});
});
});
@ -26,6 +27,7 @@ describe('create()', () => {
parentId: 'greq_124',
pinned: true,
type: 'GrpcRequestMeta',
lastActive: 0,
};
expect(request).toEqual(expected);
@ -43,15 +45,21 @@ describe('create()', () => {
parentId: 'greq_124',
pinned: false,
type: 'GrpcRequestMeta',
lastActive: 0,
};
expect(request).toEqual(expected);
});
it('fails when missing parentId', async () => {
Date.now = jest.fn().mockReturnValue(1478795580200);
expect(() => models.grpcRequestMeta.create({ pinned: true })).toThrow(
'New GrpcRequestMeta missing `parentId`',
);
});
it('fails when parentId prefix is not that of a GrpcRequest', async () => {
expect(() => models.grpcRequestMeta.create({ parentId: 'req_123' })).toThrow(
'Expected the parent of GrpcRequestMeta to be a GrpcRequest',
);
});
});

View File

@ -0,0 +1,18 @@
import * as models from '../index';
import { globalBeforeEach } from '../../__jest__/before-each';
describe('create()', () => {
beforeEach(globalBeforeEach);
it('fails when missing parentId', async () => {
expect(() => models.requestMeta.create({ pinned: true })).toThrow(
'New RequestMeta missing `parentId`',
);
});
it('fails when parentId prefix is not that of a Request', async () => {
expect(() => models.requestMeta.create({ parentId: 'greq_123' })).toThrow(
'Expected the parent of RequestMeta to be a Request',
);
});
});

View File

@ -1,6 +1,7 @@
// @flow
import * as db from '../common/database';
import type { BaseModel } from './index';
import { prefix as grpcRequestPrefix } from './grpc-request';
export const name = 'gRPC Request Meta';
export const type = 'GrpcRequestMeta';
@ -10,6 +11,7 @@ export const canSync = false;
type BaseGrpcRequestMeta = {
pinned: boolean,
lastActive: number,
};
export type GrpcRequestMeta = BaseModel & BaseGrpcRequestMeta;
@ -17,6 +19,7 @@ export type GrpcRequestMeta = BaseModel & BaseGrpcRequestMeta;
export function init() {
return {
pinned: false,
lastActive: 0,
};
}
@ -29,6 +32,10 @@ export function create(patch: $Shape<GrpcRequestMeta> = {}): Promise<GrpcRequest
throw new Error('New GrpcRequestMeta missing `parentId`');
}
if (!patch.parentId.startsWith(`${grpcRequestPrefix}_`)) {
throw new Error('Expected the parent of GrpcRequestMeta to be a GrpcRequest');
}
return db.docCreate(type, patch);
}
@ -53,6 +60,17 @@ export async function getOrCreateByParentId(parentId: string): Promise<GrpcReque
return create({ parentId });
}
export async function updateOrCreateByParentId(parentId: string, patch: $Shape<GrpcRequestMeta>) {
const requestMeta = await getByParentId(parentId);
if (requestMeta) {
return update(requestMeta, patch);
} else {
const newPatch = Object.assign({ parentId }, patch);
return create(newPatch);
}
}
export function all(): Promise<Array<GrpcRequestMeta>> {
return db.all(type);
}

View File

@ -2,6 +2,7 @@
import * as db from '../common/database';
import { PREVIEW_MODE_FRIENDLY } from '../common/constants';
import type { BaseModel } from './index';
import { prefix as requestPrefix } from './request';
export const name = 'Request Meta';
export const type = 'RequestMeta';
@ -46,6 +47,10 @@ export function create(patch: $Shape<RequestMeta> = {}) {
throw new Error('New RequestMeta missing `parentId` ' + JSON.stringify(patch));
}
if (!patch.parentId.startsWith(`${requestPrefix}_`)) {
throw new Error('Expected the parent of RequestMeta to be a Request');
}
return db.docCreate(type, patch);
}
@ -67,6 +72,17 @@ export async function getOrCreateByParentId(parentId: string) {
return create({ parentId });
}
export async function updateOrCreateByParentId(parentId: string, patch: $Shape<RequestMeta>) {
const requestMeta = await getByParentId(parentId);
if (requestMeta) {
return update(requestMeta, patch);
} else {
const newPatch = Object.assign({ parentId }, patch);
return create(newPatch);
}
}
export function all(): Promise<Array<RequestMeta>> {
return db.all(type);
}

View File

@ -56,12 +56,14 @@ class RequestCreateModal extends PureComponent {
const requestName = this._input.value;
if (selectedMethod === METHOD_GRPC) {
showModal(ProtoFilesModal, {
onSave: async protofileId => {
// TODO: Create new grpc request with the name and selected proto file - INS-198
console.log(`Create request name: [${requestName}], and protofileId: [${protofileId}]`);
onSave: async (protoFileId: string) => {
const createdRequest = await models.grpcRequest.create({
parentId,
name: requestName,
protoFileId,
});
const createdRequestId = 'gr_123';
this._onComplete(createdRequestId);
this._onComplete(createdRequest._id);
},
});
} else {

View File

@ -27,7 +27,7 @@ const ProtoFileList = ({
{!protoFiles.length && <ListGroupItem>No proto files exist for this workspace</ListGroupItem>}
{protoFiles.map(p => (
<ProtoFileListItem
key={p.id}
key={p._id}
protoFile={p}
isSelected={p._id === selectedId}
handleSelect={handleSelect}

View File

@ -497,12 +497,12 @@ class App extends PureComponent {
}
static async _updateRequestMetaByParentId(requestId, patch) {
const requestMeta = await models.requestMeta.getByParentId(requestId);
if (requestMeta) {
return models.requestMeta.update(requestMeta, patch);
const isGrpcRequest = requestId.startsWith(`${models.grpcRequest.prefix}_`);
if (isGrpcRequest) {
return models.grpcRequestMeta.updateOrCreateByParentId(requestId, patch);
} else {
const newPatch = Object.assign({ parentId: requestId }, patch);
return models.requestMeta.create(newPatch);
return models.requestMeta.updateOrCreateByParentId(requestId, patch);
}
}