import { IPublicSignupTokenStore } from '../../lib/types/stores/public-signup-token-store'; import { PublicSignupTokenSchema } from '../../lib/openapi/spec/public-signup-token-schema'; import { IPublicSignupTokenCreate } from '../../lib/types/models/public-signup-token'; export default class FakePublicSignupStore implements IPublicSignupTokenStore { tokens: PublicSignupTokenSchema[] = []; async addTokenUser(secret: string, userId: number): Promise { this.get(secret).then((token) => token.users.push({ id: userId })); return Promise.resolve(); } async get(secret: string): Promise { const token = this.tokens.find((t) => t.secret === secret); return Promise.resolve(token); } async isValid(secret: string): Promise { const token = this.tokens.find((t) => t.secret === secret); return Promise.resolve(token && new Date(token.expiresAt) > new Date()); } async count(): Promise { return Promise.resolve(0); } // eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars async delete(secret: string): Promise { return Promise.resolve(undefined); } async getAllActive(): Promise { return Promise.resolve(this.tokens); } async create( newToken: IPublicSignupTokenCreate, ): Promise { return this.insert(newToken); } async insert( newToken: IPublicSignupTokenCreate, ): Promise { const token = { secret: 'some-secret', expiresAt: newToken.expiresAt.toISOString(), createdAt: new Date().toISOString(), users: [], url: 'some=url', name: newToken.name, role: { name: 'Viewer', type: '', id: 1, }, createdBy: newToken.createdBy, }; this.tokens.push(token); return Promise.resolve(token); } async setExpiry( secret: string, expiresAt: Date, ): Promise { const token = await this.get(secret); token.expiresAt = expiresAt.toISOString(); return Promise.resolve(token); } async deleteAll(): Promise { return Promise.resolve(undefined); } destroy(): void {} async exists(key: string): Promise { return this.tokens.some((t) => t.secret === key); } // eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars async getAll(query?: Object): Promise { return Promise.resolve(this.tokens); } }