Skip to content

Commit

Permalink
community: convert js to ts
Browse files Browse the repository at this point in the history
Create a community.ts model from the community.js model.
  • Loading branch information
Mersho committed Sep 6, 2023
1 parent f57ea9f commit 4753432
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 56 deletions.
56 changes: 0 additions & 56 deletions models/community.js

This file was deleted.

85 changes: 85 additions & 0 deletions models/community.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import mongoose, { Document, Schema, Types } from 'mongoose';

const arrayLimits = (val: any[]): boolean => {
return val.length > 0 && val.length <= 2;
};

const currencyLimits = (val: string): boolean => {
return val.length > 0 && val.length < 10;
};


export interface IOrderChannel extends Document {
name: string;
type: string;
}

const OrderChannelSchema = new Schema<IOrderChannel>({
name: { type: String, required: true, trim: true },
type: {
type: String,
enum: ['buy', 'sell', 'mixed'],
},
});

export interface IUsernameId extends Document {
id: string;
username: string;
}

const usernameIdSchema = new Schema<IUsernameId>({
id: { type: String, required: true },
username: { type: String, required: true, trim: true },
});

export interface ICommunity extends Document {
name: string;
creator_id: string;
group: string;
order_channels: Types.DocumentArray<IOrderChannel>;
fee: number;
earnings: number;
orders_to_redeem: number;
dispute_channel: string;
solvers: Types.DocumentArray<IUsernameId>;
banned_users: Types.DocumentArray<IUsernameId>;
public: boolean;
currencies: Array<string>;
created_at: Date;
nostr_public_key: string;
}

const CommunitySchema = new Schema<ICommunity>({
name: {
type: String,
unique: true,
maxlength: 30,
trim: true,
required: true,
},
creator_id: { type: String },
group: { type: String, trim: true }, // group Id or public name
order_channels: {
// array of Id or public name of channels
type: [OrderChannelSchema],
validate: [arrayLimits, '{PATH} is not within limits'],
},
fee: { type: Number, min: 0, max: 100, default: 0 },
earnings: { type: Number, default: 0 }, // Sats amount to be paid to the community
orders_to_redeem: { type: Number, default: 0 }, // Number of orders calculated to be redeemed
dispute_channel: { type: String }, // Id or public name, channel to send new disputes
solvers: [usernameIdSchema], // users that are dispute solvers
banned_users: [usernameIdSchema], // users that are banned from the community
public: { type: Boolean, default: true },
currencies: {
type: [String],
required: true,
trim: true,
validate: [currencyLimits, '{PATH} is not within limits'],
},
created_at: { type: Date, default: Date.now },
nostr_public_key: { type: String },
});


module.exports = mongoose.model<ICommunity>('Community', CommunitySchema);

0 comments on commit 4753432

Please sign in to comment.