transfer-key.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import type { HydratedDocument, Model } from 'mongoose';
  2. import { Schema } from 'mongoose';
  3. import type { ITransferKey } from '~/interfaces/transfer-key';
  4. import { getOrCreateModel } from '../util/mongoose-utils';
  5. interface ITransferKeyMethods {
  6. findOneActiveTransferKey(
  7. key: string,
  8. ): Promise<HydratedDocument<ITransferKey, ITransferKeyMethods> | null>;
  9. }
  10. type TransferKeyModel = Model<ITransferKey, any, ITransferKeyMethods>;
  11. const schema = new Schema<ITransferKey, TransferKeyModel, ITransferKeyMethods>(
  12. {
  13. expireAt: { type: Date, default: () => new Date(), expires: '30m' },
  14. keyString: { type: String, unique: true }, // original key string
  15. key: { type: String, unique: true },
  16. },
  17. {
  18. timestamps: {
  19. createdAt: true,
  20. updatedAt: false,
  21. },
  22. },
  23. );
  24. // TODO: validate createdAt
  25. schema.statics.findOneActiveTransferKey = async function (
  26. key: string,
  27. ): Promise<HydratedDocument<ITransferKey, ITransferKeyMethods> | null> {
  28. return this.findOne({ key });
  29. };
  30. export default getOrCreateModel('TransferKey', schema);