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

feat(#95): can rename a line in a production #98

Merged
merged 2 commits into from
Jun 25, 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
69 changes: 68 additions & 1 deletion src/api_productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
SessionResponse,
SdpAnswer,
NewProductionLine,
ErrorResponse
ErrorResponse,
PatchLineResponse,
PatchLine
} from './models';
import { SmbProtocol } from './smb';
import { ProductionManager } from './production_manager';
Expand Down Expand Up @@ -269,6 +271,71 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
);

fastify.patch<{
Params: { productionId: string; lineId: string };
Body: PatchLine;
Reply: PatchLineResponse | ErrorResponse | string;
}>(
'/production/:productionId/line/:lineId',
{
schema: {
description: 'Modify an existing Production line.',
body: PatchLine,
response: {
200: PatchLineResponse,
400: ErrorResponse,
404: ErrorResponse,
500: Type.String()
}
}
},
async (request, reply) => {
try {
const { productionId, lineId } = request.params;
let production;
try {
production = await productionManager.requireProduction(
parseInt(productionId, 10)
);
} catch (err) {
console.warn(
'Trying to patch a production line in a production that does not exist'
);
}
if (!production) {
reply
.code(404)
.send({ message: `Production with id ${productionId} not found` });
} else {
const line = productionManager.getLine(production.lines, lineId);
if (!line) {
reply
.code(404)
.send({ message: `Line with id ${lineId} not found` });
} else {
const updatedProduction =
await productionManager.updateProductionLine(
production,
lineId,
request.body.name
);
if (!updatedProduction) {
reply.code(400).send({
message: `Failed to update line with id ${lineId} in production ${productionId}`
});
} else {
reply.code(200).send({ name: request.body.name, id: lineId });
}
}
}
} catch (err) {
reply
.code(500)
.send('Exception thrown when trying to get line: ' + err);
}
}
);

fastify.delete<{
Params: { productionId: string; lineId: string };
Reply: string | ErrorResponse;
Expand Down
5 changes: 5 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type DetailedProductionResponse = Static<
>;
export type Line = Static<typeof Line>;
export type LineResponse = Static<typeof LineResponse>;
export type PatchLine = Static<typeof PatchLine>;
export type PatchLineResponse = Static<typeof PatchLineResponse>;
export type SmbEndpointDescription = Static<typeof SmbEndpointDescription>;
export type DetailedConference = Static<typeof DetailedConference>;
export type Endpoint = Static<typeof Endpoint>;
Expand Down Expand Up @@ -180,6 +182,9 @@ export const LineResponse = Type.Object({
participants: Type.Array(UserResponse)
});

export const PatchLine = Type.Omit(Line, ['id', 'smbConferenceId']);
export const PatchLineResponse = Type.Omit(Line, ['smbConferenceId']);

export const Production = Type.Object({
_id: Type.Number(),
name: Type.String(),
Expand Down
77 changes: 77 additions & 0 deletions src/production_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ jest.mock('./db_manager', () => ({
addProduction: jest.fn(),
getProduction: jest.fn(),
getProductions: jest.fn(),
updateProduction: jest.fn(),
deleteProduction: jest.fn()
}));

Expand Down Expand Up @@ -251,4 +252,80 @@ describe('production_manager', () => {
expect(userSession?.isActive).toStrictEqual(true);
expect(userSession?.isExpired).toStrictEqual(false);
});

describe('production_manager', () => {
beforeEach(() => {
jest.resetAllMocks();
});

it('add a line to a production', async () => {
const { getProduction, updateProduction } =
jest.requireMock('./db_manager');
getProduction.mockReturnValueOnce(structuredClone(existingProduction));

const productionManagerTest = new ProductionManager();
const production = await productionManagerTest.getProduction(1);
if (production) {
await productionManagerTest.addProductionLine(production, 'newName');
expect(updateProduction).toHaveBeenLastCalledWith({
_id: 1,
name: 'productionname',
lines: [
{
name: 'linename',
id: '1',
smbConferenceId: 'smbineid'
},
{
name: 'newName',
id: '2',
smbConferenceId: ''
}
]
});
}
});

it('remove a line from a production', async () => {
const { getProduction, updateProduction } =
jest.requireMock('./db_manager');
getProduction.mockReturnValueOnce(structuredClone(existingProduction));
const productionManagerTest = new ProductionManager();
const production = await productionManagerTest.getProduction(1);
if (production) {
await productionManagerTest.deleteProductionLine(production, '1');
expect(updateProduction).toHaveBeenLastCalledWith({
_id: 1,
name: 'productionname',
lines: []
});
}
});

it('change the name of a line in a production', async () => {
const { getProduction, updateProduction } =
jest.requireMock('./db_manager');
getProduction.mockReturnValueOnce(structuredClone(existingProduction));
const productionManagerTest = new ProductionManager();
const production = await productionManagerTest.getProduction(1);
if (production) {
await productionManagerTest.updateProductionLine(
production,
'1',
'newName'
);
expect(updateProduction).toHaveBeenLastCalledWith({
_id: 1,
name: 'productionname',
lines: [
{
name: 'newName',
id: '1',
smbConferenceId: 'smbineid'
}
]
});
}
});
});
});
13 changes: 13 additions & 0 deletions src/production_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ export class ProductionManager extends EventEmitter {
return dbManager.updateProduction(production);
}

async updateProductionLine(
production: Production,
lineId: string,
lineName: string
): Promise<Production | undefined> {
const line = production.lines.find((line) => line.id === lineId);
if (line) {
line.name = lineName;
return dbManager.updateProduction(production);
}
return undefined;
}

async deleteProductionLine(
production: Production,
lineId: string
Expand Down
Loading