Skip to content

Commit 5bb4098

Browse files
committed
fix: support admin-created accounts without GitHub
1 parent 4c6c522 commit 5bb4098

7 files changed

Lines changed: 53 additions & 8 deletions

File tree

‎.env.docker.development.example‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,3 @@ TARGET_HW_DEVICE_FIRMWARE=v2.0 # Same format as in gp_probes.hardwareDeviceFirmw
8080

8181
# Are updated during `pnpm run init`
8282
AUTH_GITHUB_DEFAULT_ROLE_ID=
83-
AUTH_DISABLE_DEFAULT=false

‎.env.docker.e2e.example‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,3 @@ TARGET_HW_DEVICE_FIRMWARE=v2.0 # Same format as in gp_probes.hardwareDeviceFirmw
8383

8484
# Are updated during `pnpm run init`
8585
AUTH_GITHUB_DEFAULT_ROLE_ID=
86-
AUTH_DISABLE_DEFAULT=false

‎.env.docker.production.example‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,3 @@ TARGET_HW_DEVICE_FIRMWARE=v2.0 # Same format as in gp_probes.hardwareDeviceFirmw
8484

8585
# Are updated during `pnpm run init`
8686
AUTH_GITHUB_DEFAULT_ROLE_ID=
87-
AUTH_DISABLE_DEFAULT=

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ pnpm run seed
5353
## Prod first deploy
5454

5555
1. copy `.env.scripts.development.example` to `.env.scripts.production` and fulfill all empty values except `ADMIN_ACCESS_TOKEN`.
56-
2. copy `.env.docker.production.example` to the env vars of the container and fulfill all empty values except `AUTH_GITHUB_DEFAULT_ROLE_ID` and `AUTH_DISABLE_DEFAULT`.
56+
2. copy `.env.docker.production.example` to the env vars of the container and fulfill all empty values except `AUTH_GITHUB_DEFAULT_ROLE_ID`.
5757
3. run the remote container.
5858
4. `pnpm run schema:apply:production`
5959
5. `pnpm run migrate:production`
6060
6. get default role id and set it to the `AUTH_GITHUB_DEFAULT_ROLE_ID` env var
6161
7. restart the container
62-
8. login using github. Re-login as admin and give github user admin rights. Then set that value `AUTH_DISABLE_DEFAULT=true`. Then restart the container
62+
8. login using github. Re-login as admin and give github user admin rights.
6363
9. Login using github. Generate a static access token for your user and save it to the local .env file as `ADMIN_ACCESS_TOKEN`
6464

6565
### Prod updates

‎snapshots/collections-schema.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ fields:
735735
max_length: 255
736736
numeric_precision: null
737737
numeric_scale: null
738-
is_nullable: false
738+
is_nullable: true
739739
is_unique: false
740740
is_indexed: false
741741
is_primary_key: false

‎src/extensions/operations/adopted-probes-credits-cron-handler/src/repositories/directus.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type AdoptedProbe = {
1010

1111
type User = {
1212
id: string;
13-
external_identifier: string;
13+
external_identifier: string | null;
1414
};
1515

1616
export const getAdoptedProbes = async ({ services, getSchema }: OperationContext) => {
@@ -51,8 +51,13 @@ export const addProbeCredits = async (adoptedProbes: AdoptedProbe[], { services,
5151

5252
const users = await usersService.readMany(adoptedProbes.map(({ userId }) => userId)) as User[];
5353
const usersMap = new Map(users.map(user => [ user.id, user ]));
54+
const eligibleProbes = adoptedProbes.filter(({ userId }) => usersMap.get(userId)?.external_identifier);
5455

55-
const result = await creditsAdditionsService.createMany(adoptedProbes.map(({ id, userId, ip, name }) => ({
56+
if (eligibleProbes.length === 0) {
57+
return [];
58+
}
59+
60+
const result = await creditsAdditionsService.createMany(eligibleProbes.map(({ id, userId, ip, name }) => ({
5661
github_id: usersMap.get(userId)?.external_identifier,
5762
amount: parseInt(env.CREDITS_PER_ADOPTED_PROBE_DAY, 10),
5863
reason: 'adopted_probe',

‎src/extensions/operations/adopted-probes-credits-cron-handler/test/api.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,49 @@ describe('Adopted probes status cron handler', () => {
7575
expect(result).to.equal('Created credits with ids: 1');
7676
});
7777

78+
it('should skip non-GitHub owners without blocking other owners credits or the online hours reset', async () => {
79+
readByQuery.resolves([
80+
{ id: '1', ip: '1.2.3.4', userId: 'github-user', onlineTimesToday: 120 },
81+
{ id: '2', ip: '1.2.3.5', userId: 'non-github-user', onlineTimesToday: 120 },
82+
]);
83+
84+
readMany.resolves([
85+
{ id: 'github-user', external_identifier: '123456' },
86+
{ id: 'non-github-user', external_identifier: null },
87+
]);
88+
89+
createMany.resolves([ 1 ]);
90+
91+
const result = await operationApi.handler({}, { data, database, env, getSchema, services, logger, accountability });
92+
93+
expect(createMany.args[0]).to.deep.equal([ [{
94+
github_id: '123456',
95+
amount: 100,
96+
reason: 'adopted_probe',
97+
meta: { id: '1', ip: '1.2.3.4', name: null },
98+
adopted_probe: '1',
99+
}] ]);
100+
101+
expect(updateByQuery.args[0]).to.deep.equal([{}, { onlineTimesToday: 0 }, { emitEvents: false }]);
102+
expect(result).to.equal('Created credits with ids: 1');
103+
});
104+
105+
it('should reset online hours without creating credits when all owners lack GitHub IDs', async () => {
106+
readByQuery.resolves([
107+
{ id: '1', ip: '1.2.3.4', userId: 'non-github-user', onlineTimesToday: 120 },
108+
]);
109+
110+
readMany.resolves([
111+
{ id: 'non-github-user', external_identifier: null },
112+
]);
113+
114+
const result = await operationApi.handler({}, { data, database, env, getSchema, services, logger, accountability });
115+
116+
expect(createMany.callCount).to.equal(0);
117+
expect(updateByQuery.args[0]).to.deep.equal([{}, { onlineTimesToday: 0 }, { emitEvents: false }]);
118+
expect(result).to.equal('No credits created');
119+
});
120+
78121
it('should not assign credits for <20 hours online and still reset online hours', async () => {
79122
readByQuery.resolves([{
80123
id: '1',

0 commit comments

Comments
 (0)