@@ -12,27 +12,34 @@ const defaultPopulate = [
1212 } ,
1313] ;
1414
15+ /**
16+ * Resolves the identifier from a subscription object or raw id.
17+ * @param {Object|string } value - The subscription object or id string.
18+ * @returns {string|undefined } The resolved id.
19+ */
20+ const resolveId = ( value ) => value ?. _id || value ?. id || value ;
21+
1522/**
1623 * @function list
1724 * @description Data access operation to fetch all subscriptions from the database with an optional filter.
1825 * @param {Object } [filter] - Optional filter to apply to the query.
19- * @returns {Array } An array of subscriptions.
26+ * @returns {Promise< Array> } A promise resolving to an array of subscriptions.
2027 */
2128const list = ( filter ) => Subscription . find ( filter ) . populate ( defaultPopulate ) . sort ( '-createdAt' ) . exec ( ) ;
2229
2330/**
2431 * @function create
2532 * @description Data access operation to create a new subscription in the database.
2633 * @param {Object } subscription - The subscription object to create.
27- * @returns {Object } The created subscription.
34+ * @returns {Promise< Object> } A promise resolving to the created subscription.
2835 */
2936const create = ( subscription ) => new Subscription ( subscription ) . save ( ) . then ( ( doc ) => doc . populate ( defaultPopulate ) ) ;
3037
3138/**
3239 * @function get
3340 * @description Data access operation to fetch a single subscription by its ID.
3441 * @param {String } id - The ID of the subscription to fetch.
35- * @returns {Object } The retrieved subscription or null if the ID is not valid.
42+ * @returns {Promise< Object|null> } A promise resolving to the retrieved subscription or null if the ID is not valid.
3643 */
3744const get = ( id ) => {
3845 if ( ! mongoose . Types . ObjectId . isValid ( id ) ) return null ;
@@ -43,11 +50,14 @@ const get = (id) => {
4350 * @function update
4451 * @description Data access operation to update an existing subscription in the database.
4552 * @param {Object } subscription - The subscription object containing the updated details.
46- * @returns {Object } The updated subscription.
53+ * @returns {Promise< Object> } A promise resolving to the updated subscription.
4754 */
4855const update = ( subscription ) => {
49- if ( subscription . _id ) {
50- return Subscription . findByIdAndUpdate ( subscription . _id , subscription , { returnDocument : 'after' , runValidators : true } )
56+ const id = resolveId ( subscription ) ;
57+ if ( id && mongoose . Types . ObjectId . isValid ( id ) ) {
58+ // eslint-disable-next-line no-unused-vars
59+ const { _id, id : _virtualId , ...payload } = subscription ;
60+ return Subscription . findByIdAndUpdate ( id , payload , { returnDocument : 'after' , runValidators : true } )
5161 . populate ( defaultPopulate )
5262 . exec ( ) ;
5363 }
@@ -58,15 +68,19 @@ const update = (subscription) => {
5868 * @function remove
5969 * @description Data access operation to delete a single subscription by its ID.
6070 * @param {Object } subscription - The subscription object to delete.
61- * @returns {Object } A confirmation of the deletion.
71+ * @returns {Promise< Object|null> } A promise resolving to a confirmation of the deletion or null if invalid .
6272 */
63- const remove = ( subscription ) => Subscription . deleteOne ( { _id : subscription . id } ) . exec ( ) ;
73+ const remove = ( subscription ) => {
74+ const id = resolveId ( subscription ) ;
75+ if ( ! id || ! mongoose . Types . ObjectId . isValid ( id ) ) return null ;
76+ return Subscription . deleteOne ( { _id : id } ) . exec ( ) ;
77+ } ;
6478
6579/**
6680 * @function findByOrganization
6781 * @description Data access operation to fetch a subscription by organization ID.
6882 * @param {String } organizationId - The organization ID.
69- * @returns {Object } The retrieved subscription or null.
83+ * @returns {Promise< Object|null> } A promise resolving to the retrieved subscription or null.
7084 */
7185const findByOrganization = ( organizationId ) => {
7286 if ( ! mongoose . Types . ObjectId . isValid ( organizationId ) ) return null ;
@@ -77,10 +91,12 @@ const findByOrganization = (organizationId) => {
7791 * @function findByStripeCustomerId
7892 * @description Data access operation to fetch a subscription by Stripe customer ID.
7993 * @param {String } stripeCustomerId - The Stripe customer ID.
80- * @returns {Object } The retrieved subscription or null.
94+ * @returns {Promise< Object|null> } A promise resolving to the retrieved subscription or null.
8195 */
82- const findByStripeCustomerId = ( stripeCustomerId ) =>
83- Subscription . findOne ( { stripeCustomerId } ) . populate ( defaultPopulate ) . exec ( ) ;
96+ const findByStripeCustomerId = ( stripeCustomerId ) => {
97+ if ( ! stripeCustomerId || ! stripeCustomerId . trim ( ) ) return null ;
98+ return Subscription . findOne ( { stripeCustomerId } ) . populate ( defaultPopulate ) . exec ( ) ;
99+ } ;
84100
85101export default {
86102 list,
0 commit comments