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

Add meta to bot. #241

Merged
merged 1 commit into from
Apr 3, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Bot" ADD COLUMN "meta" JSONB;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ model Bot {
status BotStatus @default(DRAFT)
tags String[]
botImage String?
meta Json?
}

model UserSegment {
Expand Down
12 changes: 12 additions & 0 deletions src/modules/bot/bot.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const mockBotData: Prisma.BotGetPayload<{
"status": "ENABLED",
"tags": [],
"botImage": 'testBotImage',
"meta": {},
"users": [
{
"id": "testUserId",
Expand Down Expand Up @@ -355,4 +356,15 @@ describe('BotController', () => {
.rejects
.toThrowError(new BadRequestException(`'botId' is required!`));
});

it('update passes meta', async () => {
updateParametersPassed = [
'meta',
];
const resp = await botController.update('testBotId', {
'meta': {'myKey': 'myValue'}
});
expect(resp).toBeTruthy();
updateParametersPassed = [];
});
});
3 changes: 2 additions & 1 deletion src/modules/bot/bot.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ export class BotController {
"ownerID",
"ownerOrgID",
"purpose",
"description"
"description",
"meta"
];
const updateBotDto = Object.entries(body).reduce((acc, [key, value]) => {
if (value !== undefined && fieldsToInclude.includes(key)) {
Expand Down
32 changes: 32 additions & 0 deletions src/modules/bot/bot.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const MockPrismaService = {
mockBotsDbCopy.description = createData.data.description;
if (createData.data.status)
mockBotsDbCopy.status = createData.data.status;
if (createData.data.meta)
mockBotsDbCopy.meta = createData.data.meta;
return mockBotsDbCopy;
},
findUnique: (filter) => {
Expand Down Expand Up @@ -123,6 +125,7 @@ const mockCreateBotDto: CreateBotDto & { ownerID: string; ownerOrgID: string } =
tags: [],
ownerID: "",
ownerOrgID: "",
meta: undefined
chinmoy12c marked this conversation as resolved.
Show resolved Hide resolved
};

const mockFile: Express.Multer.File = {
Expand Down Expand Up @@ -776,4 +779,33 @@ describe('BotService', () => {
expect(response!['status']).toStrictEqual('PINNED');
fetchMock.restore();
});

it('bot meta is passed to db', async () => {
fetchMock.postOnce(`${configService.get<string>('MINIO_MEDIA_UPLOAD_URL')}`, {
fileName: 'testFileName'
});
const mockCreateBotDtoCopy: CreateBotDto & { ownerID: string; ownerOrgID: string } = { ...mockCreateBotDto };
mockCreateBotDtoCopy.meta = { 'myKey': 'myValue' };
mockCreateBotDtoCopy.name = 'testBotNotExisting';
mockCreateBotDtoCopy.startingMessage = 'testBotStartingMessageNotExisting';
const response = await botService.create(mockCreateBotDtoCopy, mockFile);
assert(response != null);
expect(response!['meta']).toStrictEqual({ 'myKey': 'myValue' });
fetchMock.restore();
});

it('bot create throws when meta is of invalid type', async () => {
fetchMock.postOnce(`${configService.get<string>('MINIO_MEDIA_UPLOAD_URL')}`, {
fileName: 'testFileName'
});
const mockCreateBotDtoCopy: CreateBotDto & { ownerID: string; ownerOrgID: string } = { ...mockCreateBotDto };
//@ts-ignore
mockCreateBotDtoCopy.meta = "InvalidStringType";
mockCreateBotDtoCopy.name = 'testBotNotExisting';
mockCreateBotDtoCopy.startingMessage = 'testBotStartingMessageNotExisting';
expect(botService.create(mockCreateBotDtoCopy, mockFile))
.rejects
.toThrowError(`Required type for 'meta' is JSON, provided: 'string'`);
fetchMock.restore();
});
});
4 changes: 4 additions & 0 deletions src/modules/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ export class BotService {
if (!data.name || !data.startingMessage) {
throw new BadRequestException('Bot name is required!');
}
if (data.meta && typeof data.meta != 'object') {
throw new BadRequestException(`Required type for 'meta' is JSON, provided: '${typeof data.meta}'`);
}
const name = data.name.trim();
const startingMessage = data.startingMessage.trim();
const alreadyExists = await this.prisma.bot.findFirst({
Expand Down Expand Up @@ -250,6 +253,7 @@ export class BotService {
}),
},
botImage: resp.fileName,
meta: data.meta,
};
const prismaResult = await this.prisma.bot.create({ data: createData });
this.logger.log(`BotService::create: Bot created successfully. Time taken: ${performance.now() - startTime} milliseconds.`)
Expand Down
1 change: 1 addition & 0 deletions src/modules/bot/dto/create-bot.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export class CreateBotDto {
ownerorgid: string;
purpose: string | undefined;
description: string | undefined;
meta: Record<string, any> | undefined;
}
Loading