Skip to content

Commit

Permalink
Allow overriding role expiry without having to remove and recreate ro…
Browse files Browse the repository at this point in the history
…le (#1119)

* feat: allow overriding role expiry without having to remove and recreate role

* fix: remove some tests for outdated behavior
  • Loading branch information
niekcandaele authored Aug 17, 2024
1 parent 2658e7a commit 0ebd022
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 88 deletions.
10 changes: 1 addition & 9 deletions packages/app-api/src/controllers/PlayerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,7 @@ export class PlayerController {
) {
const service = new PlayerService(req.domainId);

try {
await service.assignRole(params.roleId, params.id, data.gameServerId, data.expiresAt);
} catch (error) {
if (error instanceof Error && error.name === 'UniqueViolationError') {
throw new errors.BadRequestError('Role already assigned');
} else {
throw error;
}
}
await service.assignRole(params.roleId, params.id, data.gameServerId, data.expiresAt);

return apiResponse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,6 @@ const tests = [
return this.client.player.playerControllerAssignRole(player.id, role.data.data.id);
},
}),
new IntegrationTest<SetupGameServerPlayers.ISetupData>({
group,
snapshot: true,
name: 'Assigning the same role twice should fail',
setup: SetupGameServerPlayers.setup,
test: async function () {
const permissions = await this.client.permissionCodesToInputs([PERMISSIONS.MANAGE_GAMESERVERS]);
const role = await this.client.role.roleControllerCreate({
name: 'Test role',
permissions,
});
const player = this.setupData.players[0];
await this.client.player.playerControllerAssignRole(player.id, role.data.data.id);
return this.client.player.playerControllerAssignRole(player.id, role.data.data.id);
},
expectedStatus: 400,
}),
new IntegrationTest<SetupGameServerPlayers.ISetupData>({
group,
snapshot: true,
Expand All @@ -58,27 +41,6 @@ const tests = [
});
},
}),
new IntegrationTest<SetupGameServerPlayers.ISetupData>({
group,
snapshot: true,
name: 'Assigning the same role for same gameserver should fail',
setup: SetupGameServerPlayers.setup,
test: async function () {
const permissions = await this.client.permissionCodesToInputs([PERMISSIONS.MANAGE_GAMESERVERS]);
const role = await this.client.role.roleControllerCreate({
name: 'Test role',
permissions,
});
const player = this.setupData.players[0];
await this.client.player.playerControllerAssignRole(player.id, role.data.data.id, {
gameServerId: this.setupData.gameServer1.id,
});
return this.client.player.playerControllerAssignRole(player.id, role.data.data.id, {
gameServerId: this.setupData.gameServer1.id,
});
},
expectedStatus: 400,
}),
new IntegrationTest<SetupGameServerPlayers.ISetupData>({
group,
snapshot: true,
Expand Down Expand Up @@ -202,6 +164,53 @@ const tests = [
},
filteredFields: ['name', 'playerId', 'steamId', 'roleId', 'gameServerId', 'epicOnlineServicesId', 'xboxLiveId'],
}),
new IntegrationTest<SetupGameServerPlayers.ISetupData>({
group,
snapshot: true,
name: 'Can override the expiry of a role assignment',
setup: SetupGameServerPlayers.setup,
test: async function () {
// Assign a role with expiry
// Assign same role with new expiry
// -> Role should have new expiry

const permissions = await this.client.permissionCodesToInputs([PERMISSIONS.MANAGE_GAMESERVERS]);

const role = await this.client.role.roleControllerCreate({
name: 'Test role',
permissions,
});

const player = this.setupData.players[0];
const expiresAt = new Date(Date.now() - 10).toISOString();
const newExpiresAt = new Date(Date.now() + 1000 * 60 * 60).toISOString();

await this.client.player.playerControllerAssignRole(player.id, role.data.data.id, {
expiresAt,
});

await this.client.player.playerControllerAssignRole(player.id, role.data.data.id, {
expiresAt: newExpiresAt,
});

const res = await this.client.player.playerControllerGetOne(player.id);
const roleAssignment = res.data.data.roleAssignments.find((a) => a.role.name === role.data.data.name);
if (!roleAssignment) throw new Error('Role assignment not found');

expect(roleAssignment.expiresAt).to.be.eq(newExpiresAt);
return res;
},
filteredFields: [
'name',
'playerId',
'steamId',
'roleId',
'gameServerId',
'epicOnlineServicesId',
'xboxLiveId',
'expiresAt',
],
}),
];

describe(group, function () {
Expand Down
15 changes: 14 additions & 1 deletion packages/app-api/src/db/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,20 @@ export class PlayerRepo extends ITakaroRepo<PlayerModel, PlayerOutputDTO, Player
gameServerId,
};

await roleOnPlayerModel.query().insert(updateObj);
const whereObj: Record<string, unknown> = {
playerId,
roleId,
};

if (gameServerId) whereObj.gameServerId = gameServerId;

const existing = await roleOnPlayerModel.query().findOne(whereObj);

if (existing) {
await roleOnPlayerModel.query().update(updateObj).where(whereObj);
} else {
await roleOnPlayerModel.query().insert(updateObj);
}
}

async removeRole(playerId: string, roleId: string, gameServerId?: string): Promise<void> {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"body": {
"meta": {},
"data": {
"createdAt": "2024-08-17T14:54:58.892Z",
"updatedAt": "2024-08-17T14:54:59.406Z",
"id": "0bc2831c-42fb-4bc8-876d-37a7f789cf63",
"name": "Candice_Berge",
"steamId": "xf5eryn8r1gs02tu",
"xboxLiveId": "ne67y6p9cmjncz8m",
"epicOnlineServicesId": "x606jagm8uou2go9",
"steamAvatar": null,
"steamLastFetch": null,
"steamAccountCreated": null,
"steamCommunityBanned": null,
"steamEconomyBan": null,
"steamVacBanned": null,
"steamsDaysSinceLastBan": null,
"steamNumberOfVACBans": null,
"steamLevel": null,
"roleAssignments": [
{
"createdAt": "2024-08-17T14:54:59.664Z",
"updatedAt": "2024-08-17T14:54:59.690Z",
"id": "0a6d9fe7-e80a-410a-bf12-7095eac25143",
"playerId": "0bc2831c-42fb-4bc8-876d-37a7f789cf63",
"roleId": "e6de15d8-f192-4a90-a663-c5322f2e2d6f",
"gameServerId": null,
"expiresAt": "2024-08-17T15:54:59.646Z",
"role": {
"createdAt": "2024-08-17T14:54:59.638Z",
"updatedAt": "2024-08-17T14:54:59.639Z",
"id": "e6de15d8-f192-4a90-a663-c5322f2e2d6f",
"name": "Test role",
"system": false,
"permissions": [
{
"createdAt": "2024-08-17T14:54:59.641Z",
"updatedAt": "2024-08-17T14:54:59.641Z",
"id": "95ca3c75-4659-4be8-a1a9-3784cf84f0ea",
"roleId": "e6de15d8-f192-4a90-a663-c5322f2e2d6f",
"count": 0,
"permissionId": "3b7f6e7e-14fa-48b2-9b62-2aff6575350f"
}
]
}
},
{
"playerId": "0bc2831c-42fb-4bc8-876d-37a7f789cf63",
"roleId": "7b6d0471-cc33-4226-8155-d5584d559601",
"role": {
"createdAt": "2024-08-17T14:54:58.381Z",
"updatedAt": "2024-08-17T14:54:58.382Z",
"id": "7b6d0471-cc33-4226-8155-d5584d559601",
"name": "Player",
"system": true,
"permissions": []
}
}
],
"ipHistory": []
}
},
"status": 200,
"test": {
"group": "PlayerController",
"snapshot": true,
"name": "Can override the expiry of a role assignment",
"filteredFields": [
"name",
"playerId",
"steamId",
"roleId",
"gameServerId",
"epicOnlineServicesId",
"xboxLiveId",
"expiresAt"
],
"expectedStatus": 200,
"standardEnvironment": true
}
}

0 comments on commit 0ebd022

Please sign in to comment.