update timestamp more efficiently

This commit is contained in:
Ben Irvin 2022-10-03 12:54:19 +02:00
parent 2dadda640b
commit 7039822740
2 changed files with 11 additions and 5 deletions

View File

@ -32,16 +32,21 @@ describe('API Token Auth Strategy', () => {
'api-token': {
getBy,
hash,
update,
},
},
},
query() {
return { update };
},
};
const response = await apiTokenStrategy.authenticate(ctx);
expect(getBy).toHaveBeenCalledWith({ accessKey: 'api-token_tests-hashed-access-key' });
expect(update).toHaveBeenCalledWith(apiToken.id, { lastUsedAt: expect.any(Date) });
expect(update).toHaveBeenCalledWith({
data: { lastUsedAt: expect.any(Date) },
where: { id: apiToken.id },
});
expect(response).toStrictEqual({ authenticated: true, credentials: apiToken });
});

View File

@ -52,9 +52,10 @@ const authenticate = async (ctx) => {
}
}
// update lastUsedAt
await apiTokenService.update(apiToken.id, {
lastUsedAt: currentDate,
// update lastUsedAt async without waiting or worrying about result
await strapi.query('admin::api-token').update({
where: { id: apiToken.id },
data: { lastUsedAt: currentDate },
});
if (apiToken.type === constants.API_TOKEN_TYPE.CUSTOM) {